而原始输入的While循环在Python中给出了奇怪的输出
不管我输入什么,我得到的结果是“我认为你太老了,不能去冒险了。你的冒险到此结束。” 比如,如果我输入17,我会得到“我认为你已经老了,可以去冒险了。你的冒险到此结束。”如果我输入18-59,我会得到“我认为你已经老了,可以去冒险了。你的冒险到此结束。”而原始输入的While循环在Python中给出了奇怪的输出,python,python-2.7,input,while-loop,Python,Python 2.7,Input,While Loop,不管我输入什么,我得到的结果是“我认为你太老了,不能去冒险了。你的冒险到此结束。” 比如,如果我输入17,我会得到“我认为你已经老了,可以去冒险了。你的冒险到此结束。”如果我输入18-59,我会得到“我认为你已经老了,可以去冒险了。你的冒险到此结束。” 为True时: 年龄=原始输入(“\n告诉我”+str(name)+”,你多大了?:”) 如果age.isdigit()=False: 打印“\n请只输入数字!” 时间。睡眠(3) elif年龄=60岁: 打印“\n%s。我想你太老了,不能去冒险
为True时:
年龄=原始输入(“\n告诉我”+str(name)+”,你多大了?:”)
如果age.isdigit()=False:
打印“\n请只输入数字!”
时间。睡眠(3)
elif年龄<18岁:
打印“\n%s。辅修。你必须年满18岁才能参加这次冒险。你的冒险到此结束。”%age
时间。睡眠(7)
出口(0)
elif年龄>=60岁:
打印“\n%s。我想你太老了,不能去冒险。你的冒险到此结束。”%age
时间。睡眠(5)
出口(0)
其他:
打印“\n%s。您开始变老了。”%age
打破
您需要将输入作为整数进行比较
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
否则,您将比较str
和int
。请参见下面的示例
>>> 5 < 10
True
>>> type(5)
<type 'int'>
>>> '5' < 10
False
>>> type('5')
<type 'str'>
>>5<10
真的
>>>类型(5)
>>> '5' < 10
假的
>>>类型('5')
在Python2.x中,比较不同类型的值通常会忽略这些值,而是比较这些类型。因为
str>=int
,所以任何字符串都是=
任何整数。在Python3.x中,您会得到一个类型错误
,而不是默默地做一些令人困惑和难以调试的事情。您需要将输入作为int进行比较
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
否则,您将比较str
和int
。请参见下面的示例
>>> 5 < 10
True
>>> type(5)
<type 'int'>
>>> '5' < 10
False
>>> type('5')
<type 'str'>
>>5<10
真的
>>>类型(5)
>>> '5' < 10
假的
>>>类型('5')
在Python2.x中,比较不同类型的值通常会忽略这些值,而是比较这些类型。因为str>=int
,所以任何字符串都是=
任何整数。在Python3.x中,你会得到一个TypeError
,而不是默默地做一些令人困惑和难以调试的事情。问题是raw\u input
总是返回一个string对象,而那些对象并不能真正与int
类型进行比较。你需要做一些类型转换
如果要使用isdigit
测试输入是否为数字,则应按以下方式进行:
while True:
age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
if age.isdigit() == False:
print "\n Please, put only in numbers!"
time.sleep(3)
elif int(age) < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif int(age) >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
为True时:
年龄=原始输入(“\n告诉我”+str(name)+”,你多大了?:”)
如果age.isdigit()=False:
打印“\n请只输入数字!”
时间。睡眠(3)
elif int(年龄)<18岁:
打印“\n%s。辅修。你必须年满18岁才能参加这次冒险。你的冒险到此结束。”%age
时间。睡眠(7)
出口(0)
elif int(年龄)>=60:
打印“\n%s。我想你太老了,不能去冒险。你的冒险到此结束。”%age
时间。睡眠(5)
出口(0)
其他:
打印“\n%s。您开始变老了。”%age
打破
但是,您可以通过立即转换为整数,并在异常为无效字符串时捕获异常,从而稍微简化代码:
while True:
try:
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
if age < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif age >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
except ValueError:
print "\n Please, put only in numbers!"
time.sleep(3)
为True时:
尝试:
年龄=int(原始输入(“\n告诉我”+str(name)+”,你多大了?:”)
如果年龄<18岁:
打印“\n%s。辅修。你必须年满18岁才能参加这次冒险。你的冒险到此结束。”%age
时间。睡眠(7)
出口(0)
elif年龄>=60岁:
打印“\n%s。我想你太老了,不能去冒险。你的冒险到此结束。”%age
时间。睡眠(5)
出口(0)
其他:
打印“\n%s。您开始变老了。”%age
打破
除值错误外:
打印“\n请只输入数字!”
时间。睡眠(3)
问题在于原始输入总是返回一个字符串对象,而这些对象并不能真正与int
类型进行比较。你需要做一些类型转换
如果要使用isdigit
测试输入是否为数字,则应按以下方式进行:
while True:
age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
if age.isdigit() == False:
print "\n Please, put only in numbers!"
time.sleep(3)
elif int(age) < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif int(age) >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
为True时:
年龄=原始输入(“\n告诉我”+str(name)+”,你多大了?:”)
如果age.isdigit()=False:
打印“\n请只输入数字!”
时间。睡眠(3)
elif int(年龄)<18岁:
打印“\n%s。辅修。你必须年满18岁才能参加这次冒险。你的冒险到此结束。”%age
时间。睡眠(7)
出口(0)
elif int(年龄)>=60:
打印“\n%s。我想你太老了,不能去冒险。你的冒险到此结束。”%age
时间。睡眠(5)
出口(0)
其他:
打印“\n%s。您开始变老了。”%age
打破
但是,您可以通过立即转换为整数,并在异常为无效字符串时捕获异常,从而稍微简化代码:
while True:
try:
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
if age < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif age >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
except ValueError:
print "\n Please, put only in numbers!"
time.sleep(3)
为True时:
尝试:
年龄=int(原始输入(“\n告诉我”+str(name)+”,你多大了?:”)
如果年龄<18岁:
打印“\n%s。辅修。你必须年满18岁才能参加这次冒险。你的冒险到此结束。”%age
时间。睡眠(7)
出口(0)
elif年龄>=60岁:
打印“\n%s。我想你太老了,不能去冒险。你的冒险到此结束。”%age
时间。睡眠(5)
出口(0)
其他:
打印“\n%s。您开始变老了。”%age
打破
除值错误外:
打印“\n请只输入数字!”
时间。睡眠(3)
raw\u input
每次都会给您一个字符串,因此您无法将其与数字进行比较。请尝试int(原始输入(…)
raw\u input
每次都会给您一个字符串,因此您无法将其与数字进行比较。尝试int(原始输入(…)
。一个问题是:他正在检查age.isdigit
,因此现在将其转换为int
,为时过早。您需要将转换移动到第一个下的else
子句中