Python-Learning-Day1

第二章 Python 判断语句

一.布尔类型和比较运算符

布尔类型的字面量:
• True 表示真(是、肯定)
• False 表示假 (否、否定)
定义变量存储布尔类型数据:
变量名称 = 布尔类型字面量

布尔类型不仅可以自行定义,同时也可以通过计算的来。
也就是使用比较运算符进行比较运算得到布尔类型的结果。

result = 10>5
print (f"the result of 10>5:{result},the type is:{type(result)}")

output:
Ture,<class ‘bool’>

二.if 语句的基本格式

  1. if 语句的基本格式:
if xxxx:
    xxxxxx
    xxxxxx
    xxxxxx
  1. if 语句的注意事项:
• 判断条件的结果一定要是布尔类型
• 不要忘记判断条件后的: 引号
• 归属于 if 语句的代码块,需在前方填充 4 个空格缩进

三.if else语句

if xxxx:
    xxxxxx
    xxxxxx
    xxxxxx
else:
    xxxxxx
    xxxxxx
    xxxxxx
xxxx

四.if elif else语句

if xxxx:
    xxxxxx
    xxxxxx
    xxxxxx
elif 1xxxx:
    xxxxxx
    xxxxxx
    xxxxxx
elif 2xxxx:
    xxxxxx
    xxxxxx
    xxxxxx
else:
    xxxxxx
    xxxxxx
    xxxxxx
xxxx

判断是互斥且有顺序的。
• 满足 条件1 将不会理会
2 和 3
• 满足 2 ,将不会理会 3
• 1 、 2 、 3 均不满足,进入 else

使用 if elif else 的注意点有:
• elif 可以写多个
• 判断是互斥且有序的,上一个满足后面的就不会判断了
• 可以在条件判断中,直接写 input 语句,节省代码量

if int(input("please input the first num you guess:"))==10:
    print("Yes,congratulations,the num is 10")
elif int(input("wrong num,please try again:"))==10:
    print("Yes,congratulations,the num is 10")
elif int(input("wrong num,please try for the last time:"))==10:
    print("Yes,congratulations,the num is 10")
else:
    print("Sorry,you have all guess wrong,the num I thought is 10")

if int(input(“please input the first num you guess:”))==10:
千万要记得这里输入的10是str类型,如果不加int转换类型,那即使输入了10也会说是错的。

五.判断语句的嵌套

Description:

Write a Python program that determines whether a person is eligible to receive gifts based on their age, length of employment, and grade. The eligibility criteria are as follows:

1.	The person must be an adult aged between 18 and 30 years.
2.	They must meet one of the following conditions:
•	Have a length of employment of more than 2 years.
•	Have a grade higher than 3.
3.	If the person does not meet the above criteria, provide appropriate messages based on their age and employment length.
age = int(input("Your age is: "))
years = int(input("Your length of employment is: "))
grade = int(input("Your grade is: "))

if 18 <= age < 30:
    if years >= 2:
        print("Your age is between 18 and 30 and your length of employment is over 2 years. Please receive the gifts.")
    elif grade > 3:
        print("Your age is between 18 and 30 and your grade is over 3. Please receive the gifts.")
    else:
        print("Sorry, your length of employment isn't meeting the standard as well as your age.")
else:
    if age >= 30:
        print("Sorry, you are over the age we set.")
    else:
        print("Sorry, minors are not eligible to receive gifts.")

实战案例
Guess the Random Number

Task:

Define a number randomly generated between 1 and 10, and use three attempts to guess the number through conditional statements.

Requirements:

1.	The number should be randomly generated within the range of 1-10.
2.	Provide three attempts to guess the number, using conditional statements to implement the logic.
3.	For each incorrect guess, provide hints indicating whether the guess is too high or too low.

Hint:

You can define a variable num to store the randomly generated number using the following code:

import random
num = random.randint(1, 10)

Answer:

import random
num = random.randint(1,10)
a=int(input("The first chance:"))
if a==num:
    print("Congratulations,your guess is correct.")
elif a>num:
    print("the number you guess is higher than num.")
    b=int(input("The second chance:"))
    if b == num:
        print("Congratulations,your guess is correct.")
    elif b > num:
        print("the number you guess is higher than num.")
        c = int(input("The last chance:"))
        if c == num:
            print("Congratulations,your guess is correct.")
        else:
            print(f"your last guess is wrong again,the correct number is {num}")
    elif b < num:
        print("the number you guess is lower than num.")
        c = int(input("The last chance:"))
        if c == num:
            print("Congratulations,your guess is correct.")
        else:
            print(f"your last guess is wrong again,the correct number is {num}")
else:
    print("the number you guess is lower than num.")
    b = int(input("The second chance:"))
    if b == num:
        print("Congratulations,your guess is correct.")
    elif b > num:
        print("the number you guess is higher than num.")
        c = int(input("The last chance:"))
        if c == num:
            print("Congratulations,your guess is correct.")
        else:
            print(f"your last guess is wrong again,the correct number is {num}")
    elif b < num:
        print("the number you guess is lower than num.")
        c = int(input("The last chance:"))
        if c == num:
            print("Congratulations,your guess is correct.")
        else:
            print(f"your last guess is wrong again,the correct number is {num}")




  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值