如果和其他在Python中,如果年龄是真的等级是假的,这个程序打印是错误的

时间:2018-03-15 06:45:44

标签: python

age = float(raw_input("Enter your age: "))
grade = int(raw_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."

如果年龄为真且等级为假,则此程序打印错误的输出。但如果年龄为假,则打印正确的输出。

为什么会这样?

3 个答案:

答案 0 :(得分:1)

您将保留age >= 8grade < 3无法控制流量的可能性。您可以使用and语句

简洁地更正此内容
age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8 and grade >= 3:
    print "You can play this game."
else:
    print "Sorry, you can't play the game."

答案 1 :(得分:0)

age = float(raw_input("Enter your age: "))
grade = int(raw_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."
else:
    print "Sorry , you can't play the game."

答案 2 :(得分:0)

您必须在嵌套的if / else语句中包含'else'条件:

age = float(raw_input("Enter your age: "))
grade = int(raw_input("Enter your grade: "))
if age >= 8:
    if grade >= 3:
        print "You can play this game."
    else:
        print"Sorry, you can't play this game."
else:
    print "Sorry, you can't play this game."