因此,作为我在python上的第一个项目,我正在尝试制作一个二分键,它可以在提出问题后猜测你在想什么动物,我对此真的很陌生,所以试着简单地解释一下:)。如果这个问题是在别的地方被问到的,我也很抱歉,我真的不知道怎么问。
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#FUR
if think=="READY" :
fur=input ("Does it have fur?")
else :
print ("I'll be waiting")
if fur=="YES" :
legs=input ("Does it walk on four legs?") :
elif fur=="NO" :
reptile=input ("Is it a reptile?")
#REPTILE
if reptile=="YES" :
shell=input ("Does it have a shell?")
if reptile=="NO" :
fly=input ("Can it fly?")
#LEGS
if legs=="YES" :
pet=input ("Do people own it as a pet?")
if legs=="NO" :
marsupial=input("Is it a marsupial?")
我不能让它跳到“人们把它当做宠物”,如果你在腿上回答是的话。另外,“I‘’ll be work”(否则)也不起作用。哦,这是python 3.x btw。
为格式化而编辑
编辑2:去掉比较中的括号:)
发布于 2018-07-30 10:33:12
让我们从头开始:
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#FUR
if think=="READY" :
fur=input ("Does it have fur?")
else :
print ("I'll be waiting")
如果用户在存储在"think“中的第一个输入中输入"ready”之外的任何内容,那么if condition将为false,并且您的程序将直接转到第二部分中的else部分:
if fur=="YES" :
legs=input ("Does it walk on four legs?") :
elif fur=="NO" :
reptile=input ("Is it a reptile?")
它会使你的程序崩溃,因为没有名为fur的变量,而你想要将它与一些东西进行比较。
对于这种情况(等待用户输入您期望的输入),最好使用无限循环,当用户输入您期望的输入时,使用break退出。
因此,您必须将第一部分更改为:
think=input ("Think of an animal. Type ready when you want to begin")
think=think.upper()
#THINK
while True:
if think=="READY" :
fur=input ("Does it have fur?")
break
else :
print ("I'll be waiting")
对于其他部分,如上所述的事情可能会再次发生(例如,如果用户对“它用四条腿行走吗?”同样,您没有任何名为reptile的变量,您希望将其与下一行中的其他变量进行比较)
我建议使用嵌套条件:
#FUR
if fur=="YES" :
legs=input ("Does it walk on four legs?")
#LEGS
if legs=="YES" :
pet=input ("Do people own it as a pet?")
elif legs=="NO" :
marsupial=input("Is it a marsupial?")
elif fur=="NO" :
reptile=input ("Is it a reptile?")
#REPTILE
if reptile=="YES" :
shell=input ("Does it have a shell?")
if reptile=="NO" :
fly=input ("Can it fly?")
另外,别忘了:
1-清除代码这部分中的:
legs=input ("Does it walk on four legs?") :
2-如果你想从用户那里得到一个换行符,比如第一行,\n必须是有用的
think=input ("Think of an animal. Type ready when you want to begin\n")
或者甚至可以对字符串使用print (因为print会在您每次使用后自动添加一个换行符):
print("Think of an animal. Type ready when you want to begin")
think=input()
https://stackoverflow.com/questions/51583897
复制相似问题