2017-09-30 56 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的幫助