與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7

yarking207發表於2016-04-01
#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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章