2017-09-30 43 views
0

所以我试图通过选择自己的冒险游戏来学习python。我遇到的问题是我无法让用户真正选择。我写了一大堆内容,我希望让用户在进入洞1或洞2时进行选择。但是现在当用户键入1或2时,什么都不会发生。它只是一个空白的行,并没有提供1/2洞的内容。我想我遇到了一个存储用户输入然后回想起来的问题,但我不知道如何解决它。理想情况下,当用户输入1或2时,他们将看到这些洞中的情况。我想再次指出,我是一名初学者,我确信有更好的方法或更有效的方法来写这个,但我的主要焦点是让球员进入1洞或2洞。我使用Python 3.6在Spyder 3.x中。if/else issue python 3.4

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() 
+1

源代码中的'1'代表一个int。 'input'总是给你一个字符串。 – user2357112

+0

'one'是一个变量。 ''一个“'是一个字符串来比较。假设'one!=“one”有很大差异' –

+0

@ user2357112我将它改为holeInput = int(input('你想进入第一个洞还是第二个洞?输入1/2'))并且工作。谢谢! – amnmustafa15

回答

1

你应该把“或 ”周围的数字或文字:

if holeInput =='one': 

if holeInput =="one": 

因为用户输入作为一个字符串返回所以价值。“ 1 “不是数字1.

作为一个便笺,你可以检查多个正确的值(因为你告诉用户输入”一个/两个“他们可能会执行LLY型“一”也许“二”:

if holeInput=='1' or holeInput=='one': 
+1

或'如果holeInput {{1','一'}' –

+0

@Soroush谢谢。我意识到我已经混杂了我的变量,现在我知道如何标记输入/变量,如果我想让用户输入数字/文本。谢谢! – amnmustafa15

+0

是好点。实际上它应该在括号中'如果holeInput在['1','one']'中。大括号{}用于字典,而不是列表。 – Soroush

0

我改成了 holeInput = INT(输入('你想进入一个洞或孔二?键入1/2')) ,现在它会打印1/2孔的选项。谢谢@ user2357112的帮助