为什么我的elif语句在Python 3中不工作?
我正在制作一个简单的生活模拟游戏,但在第10到15行的elif语句没有正常工作,你需要输入“cry”两次,而不是一次。
def p(words):
print(words)
return words
p("Hello I'm Bob You Will Be Controlling My Life From Now On")
print ("Side Note DON'T DIE")
print ("Anyways Bye")
print (":)")
print ("You Are Born On Monday The Fourth Of March Would You Like To start To cry or stay quite")
if input() == "cry":
print ("You Cry For The Rest Of Your Life And Die Of Depression At The Age Of 12 YOU DIED RE-RUN THE PROGRAM")
exit()
elif input() == "stay quite":
p("----------------------")
print ("You Stay Quiet And Your Mother Is Very Confeused Mother: Why Are You So Quiet?. You Fell Asleep And Woke Up The Next Day At 2:34 AM")
print ("do you want to go to sleep or dance")
print ("go to sleep|dance")
if input() == "dance":
print ("You dance For The Rest Of Your Life And die from exhaustion YOU DIED RE-RUN THE PROGRAM")
exit()
else:
print ("You Go To Sleep And the next day you Wake Up you open your eyes to people looking at you and saying aww look at that cute little baby")
print ("You have the choice to either make some baby noises or tell them to go away and go to sleep")
print ("go away or make some baby noises")
if input() == "go away":
print ("you dumb baby you cant talk")
else:
print ("Everyone: Awwwwwww! You Are So Cute Bartholomew Bobbity Bob")
print ("after this you decide to go to sleep or go to the roof to paraglide")
print ("go to sleep or go to the roof")
if input() == "go to the roof":
print ("You go to the roof and paraglide and you come closer and closer to the ground and you see a bird and the bird saves you and you go home and live to tell the tale")
else:
print ("you had a nightmare and died from fright")
我刚学Python,所以不知道该怎么修复它。
2 个回答
0
把输入的内容保存到一个变量里,然后对这个变量进行if/else判断。你会发现代码是可以正常运行的。
user_input = input()
if user_input == "cry":
print ("You Cry For The Rest Of Your Life And Die Of Depression At The Age Of 12 YOU DIED RE-RUN THE PROGRAM")
exit()
elif user_input == "stay quite":
p("----------------------")
print ("You Stay Quiet And Your Mother Is Very Confeused Mother: Why Are You So Quiet?. You Fell Asleep And Woke Up The Next Day At 2:34 AM")
2
想想你在这里做的事情:
if input() == "cry":
#do something
elif input() == "stay quite":
#do something else
你怎么才能到达 elif
这个部分呢?因为 input()
是用来获取输入的,所以你需要输入值 "stay quite"
两次,因为你调用了 input()
两次。
只需要 一次 提示输入,然后把结果存储在一个变量里。比如:
choice = input()
if choice == "cry":
#do something
elif choice == "stay quite":
#do something else