#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")
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/
本文通过几个具体的Python程序示例介绍了如何使用条件语句进行逻辑判断,包括比较操作符的用法、多重条件判断及逻辑运算符的应用。
1119

被折叠的 条评论
为什么被折叠?



