age = 0
joke = 'What do you call a cow with no legs? Ground beef.'
myName = raw_input('Hello! What is your name?')
myAge = raw_input('Tell me your age, ' + myName + ', to hear a joke!')
if age < 6:
print('You are too young to hear this joke.')
elif age > 12:
print('You are too old to hear this joke.')
elif age == range(6, 12):
print(joke)
运行时的代码只说我还太年轻,即使我的数字超过6甚至更高,也无法听到这个笑话。您能帮助我解决这个问题吗?
不需要上次elif
。只需使用没有条件的else
即可。
如果age < 6
和age > 12
都评估为false,我们知道6≤a≤12。无需检查。
btw, range()
返回 itoble ,而不是可以与数字进行比较的东西。比较将始终失败,因为您正在比较两件事不相容。
您将输入分配到变量" myage"中,并在您的if语句中测试变量"年龄"。
替换
myAge = raw_input('Tell me your age, ' + myName + ', to hear a joke!')
age = input('Tell me your age, ' + myName + ', to hear a joke!')