与小卡特一起学python 第7章 判断再判断 7-1-2-3-6-7

本文通过几个具体的Python程序示例介绍了如何使用条件语句进行逻辑判断,包括比较操作符的用法、多重条件判断及逻辑运算符的应用。
#7.1使用比较操作符
num1 = float(input ("Enter the first number:"))
num2 = float(input ("Enter the second number:"))
if num1 < num2:
    print(num1,"is less than",num2)
if num1 > num2:
    print(num1,"is greater than",num2)
if num1 == num2:
    print(num1,"is equal to",num2)
if num1 != num2:
    print(num1,"is not equal to",num2)
#7.2
answer = float(input("Enter a number from 1 to 15"))
if answer >= 10:
    print("You got at least 10!")
elif answer >= 5:
    print("You got at least 5!")
elif answer >=3:
    print("You got at least 3")
else:
    print("You got less than 3.")

#7.6测试多个条件
age = float(input("Enter your age:"))
grade = int(input("Enter your grade:"))
if age > 8:
    if grade >= 3:
        print("You can play this game!")
else:
    print("Sorry.You can't play the game!)
    
#7.7 使用and,or
age = float(input("Enter your age:"))
grade = int(input("Enter your grade:"))
color = input("Enter your favorite color:")
##if age > 8 and grade >=3 and color == "green":
##    print("You can play this game!")
##else:
##    print("Sorry,you can't play the game!")
if color == "red" or color == "bule" or color == "green":
    print("You are allowed to play this game!")
else:
    print("Sorry,you can't play the game")

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2073853/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220205/viewspace-2073853/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值