if/else 期 python 3.4
if/else issues python 3.4
所以我正在尝试通过选择您自己的冒险游戏来学习 python。我遇到的问题是我无法让用户实际选择。我写了一堆内容,我想让用户选择进入第 1 洞还是第 2 洞。但是现在当用户输入 1 或 2 时,什么也没有发生。它只是转到一个空白行并显示孔 1/2 内容的 none。我想我在存储用户输入然后调用它时遇到了问题,但我不确定如何解决它。理想情况下,当用户键入 1 或 2 时,他们将看到这些洞中的情况。我想再次指出,我是初学者,我确信有更好的方法或更有效的方法来写这篇文章,但我的主要重点是让球员进入第 1 洞或第 2 洞。我正在使用 python 3.6 in spyder 3.x.
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()
您应该在数字或单词周围放置 ' 或 ":
if holeInput =='one':
或
if holeInput =="one":
因为用户输入以字符串形式返回。所以值是“1”而不是数字 1。
作为旁注,您可以检查多个正确的值(因为您告诉用户输入 "one/two" 他们实际上可能会输入 'one' 或者 'two':
if holeInput=='1' or holeInput=='one':
我改成了
holeInput = int(输入('Do you want to go into hole one or hole two? type 1/2 '))
现在它打印孔 1/2 的选项。感谢@user2357112 的帮助
所以我正在尝试通过选择您自己的冒险游戏来学习 python。我遇到的问题是我无法让用户实际选择。我写了一堆内容,我想让用户选择进入第 1 洞还是第 2 洞。但是现在当用户输入 1 或 2 时,什么也没有发生。它只是转到一个空白行并显示孔 1/2 内容的 none。我想我在存储用户输入然后调用它时遇到了问题,但我不确定如何解决它。理想情况下,当用户键入 1 或 2 时,他们将看到这些洞中的情况。我想再次指出,我是初学者,我确信有更好的方法或更有效的方法来写这篇文章,但我的主要重点是让球员进入第 1 洞或第 2 洞。我正在使用 python 3.6 in spyder 3.x.
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()
您应该在数字或单词周围放置 ' 或 ":
if holeInput =='one':
或
if holeInput =="one":
因为用户输入以字符串形式返回。所以值是“1”而不是数字 1。
作为旁注,您可以检查多个正确的值(因为您告诉用户输入 "one/two" 他们实际上可能会输入 'one' 或者 'two':
if holeInput=='1' or holeInput=='one':
我改成了 holeInput = int(输入('Do you want to go into hole one or hole two? type 1/2 ')) 现在它打印孔 1/2 的选项。感谢@user2357112 的帮助