为什么这个简单的代码错了?

时间:2016-06-12 19:21:45

标签: python python-3.x

print("Hello there")
name=input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
    print("K")
else:
    print("Sorry bruh")
print("Thanks")
输入15时,

一直向我显示“对不起”。为什么?怎么了?

3 个答案:

答案 0 :(得分:3)

input投放到int

age = int(input("Your age"))

您可以添加try-except。您的情况应同等地在or而不是and

进行评估

答案 1 :(得分:0)

要么使用Python3,要么使用int(input("Your age"))进行投射,要么使用Python2,然后需要使用raw_input来读取名称:name=raw_input("What is your name?")

答案 2 :(得分:0)

好的,所以你可以在python2.7中尝试这个代码,它可以按你的需要工作:

print("Hello there")
name=raw_input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
    print("K")
else:
    print("Sorry bruh")
print("Thanks")