所以我试图通过选择你自己的冒险游戏来学习python。我遇到的问题是我无法让用户真正选择。我已经写了很多内容,我想让用户在第1洞或第2洞之间做出选择。但是现在当用户键入1或2时,没有任何反应。它只是一个空白行,并没有提供任何孔1/2内容。我认为我存在存储用户输入然后回忆它的问题,但我不确定如何解决它。理想情况下,当用户输入1或2时,他们将会看到这些洞中的情况。我想再次指出我是初学者,我确信有更好的方法或更有效的方式来写这个,但我的主要关注点是让玩家进入第1洞或第2洞。我在spyder 3.x中使用python 3.6。
def main():
print('A giant ship comes crashing down in your swamp and ruins your possum and larvae soup.')
print('A man emerges from the swamp. Gross he says. Why would anyone live in this shithole swamp?')
print('Yoda emerges from his hut. I live here. I am Yoda, the talking swamp rat that will teach you how to kill your father.')
begin = input("Do you want my help? Q=Quit. ")
if begin.lower() !='q':
print('Good lets begin') ##!='q': this is how you give someone the option to quit.
else:
print('good luck im going to eat some larvae and possum soup')
userName = input("I have to ask. What is your name? ")
print('Nice to meet you', userName,'Skywalker')
print('Okay so first things first, lets get your ship out of my swamp. Great', userName, 'says.')
print('But I will only do it if you catch me some possums. They are a delicassy here. They are in one of those 2 holes.')
holeInput = input('Do you want to go into hole one or hole two? type one/two ')
if holeInput == one: ##why won't this work?
print('You enter the hole. It is small and you have to crawl. All of a sudden there is a bright light.')
print('You see a family of squirrels. Squirrels are not possums.')
squirrel = input("Do you bring the squirrel to Yoda and hope he does not notice or do you leave? Quit means leave. Q=Quit." )
if squirrel.lower() !='q':
print('Congrats! you are now fighting a squirrel. You kill the squirrel in one blow and bring its carcass to Yoda.')
print('You are a liar! Yoda says. I will not help you. Yoda goes inside and eats some possum and larvae soup.')
return holeInput
else:
print('You leave the hole to check the other hole.')
return holeInput
else:
return holeInput
if holeInput == two:
print('You enter the hole. It is small and you have to crawl. All of a sudden there is a bright light.')
print('You see a family of possums reading the space bible. One of the possums has glasses and a human face.')
print('The possum turns to you. I am not a possum he says. My name is George Lucas the possum says. But it could be a lie. He really looks like a possum.')
lucas = input("Do you listen to the talking possum? Quit means let him live. Q=Quit." )
if lucas.lower() !='q':
print('You kill the possum in one blow. You bring his body to Yoda. Wow! thats the biggest possum I have ever seen. You are a good guy and I will help you Yoda says.')
else:
print('You leave. Yoda calls you a failure.')
return holeInput
main()
答案 0 :(得分:1)
你应该放置'或"围绕数字或单词:
if holeInput =='one':
或
if holeInput =="one":
因为用户输入以字符串形式返回。所以价值是" 1"不是第1号。
作为旁注,您可以检查多个正确的值(因为您告诉用户输入"一个/两个"他们实际上可能会键入'一个'或者可能'两个& #39;:
if holeInput=='1' or holeInput=='one':
答案 1 :(得分:0)
我把它改成了 holeInput = int(输入('你想进入第一洞还是第二洞?输入1/2')) 现在它打印孔1/2的选项。感谢@ user2357112的帮助