Python代码仅提供第一个响应

时间:2016-04-13 01:29:24

标签: python

我已经对输入其年龄的用户编写了响应,但它只返回第一个响应。有什么想法吗?

print('Lets play a game with your age!')
int(input('Enter your age'))
if int() <= 10:
    print("You still need to wear what your Mom puts out for you!")
elif int() >= 11 and int() <= 15:
    print("Congratulations! You are old enough to chew gum and walk at the same time!")
elif int()  >= 16 and int() <= 18:
    print("You are old enough to sit in the emergency exit row of an airplane!")
elif int() >= 19 and int() <= 29:
    print("Wow! You are REALLY old!!!")
else:
    print("Your age is off the charts. Are you sure you're not a dinosaur?")

然后我做了更多研究并尝试了这个:

print('Lets play a game with your age!')
int(input('Enter your age'))
if int(input('Enter your age')) <= 10:
    print("You still need to wear what your Mom puts out for you!")
elif int(input('Enter your age')) >= 11 and int(input('Enter your age')) <= 15:
    print("Congratulations! You are old enough to chew gum and walk at the same time!")
elif int(input('Enter your age'))  >= 16 and int(input('Enter your age')) <= 18:
    print("You are old enough to sit in the emergency exit row of an airplane!")
elif int(input('Enter your age')) >= 19 and int(input('Enter your age')) <= 29:
    print("Wow! You are REALLY old!!!")
else:
    print("Your age is off the charts. Are you sure you're not a dinosaur?")

这样可行,但问题在于,根据输入的变量,它会要求输入2到8倍的年龄(每个int一个(输入(“输入你的年龄”))。

我一直在努力解决这个问题超过一个小时,这让我很生气! LOL

3 个答案:

答案 0 :(得分:1)

您需要将input()的值赋给变量。

<asp:Panel ..>

答案 1 :(得分:1)

您需要将input()值分配给变量,  所有if陈述之前的一次

print('Lets play a game with your age!')
age = int(input('Enter your age'))
if age <= 10:
    print("You still need to wear what your Mom puts out for you!")
elif age  >= 11 and age <= 15:
    print("Congratulations! You are old enough to chew gum and walk at the same time!")
elif age >= 16 and age <= 18:
    print("You are old enough to sit in the emergency exit row of an airplane!")
elif age >= 19 and age <= 29:
    print("Wow! You are REALLY old!!!")
else:
    print("Your age is off the charts. Are you sure you're not a dinosaur?")

答案 2 :(得分:0)

你要求他们输入,但是不能将它保存在任何地方。

response = int(input('Enter your age'))

然后将if int() <= 10等行替换为if response <= 10等等。

print('Lets play a game with your age!')
response = int(input('Enter your age')) # assign result of input to a variable
if response <= 10:
    print("You still need to wear what your Mom puts out for you!")
elif response >= 11 and response <= 15:
    print("Congratulations! You are old enough to chew gum and walk at the same time!")
elif response >= 16 and response <= 18:
    print("You are old enough to sit in the emergency exit row of an airplane!")
elif response >= 19 and response <= 29:
    print("Wow! You are REALLY old!!!")
else:
    print("Your age is off the charts. Are you sure you're not a dinosaur?")

了解您正在做的事情。 您每次撰写input()时,都会询问一个全新的提示。这是因为每次你做foo(args)之类的事情,你就是调用(迫使Python运行)这个函数带有参数args

input()是一个功能。它要求回复一次并且只给你一次值。每当Python看到它时,运行它并获得一个全新的值。

如果您只需要,则将其分配给新变量。该变量将记录响应作为值,并且每次使用它时,它将评估该值。