2016-05-12 135 views
0

我試圖製作一款遊戲,您可以通過迷宮嘗試逃離語音,但每次玩家都會對其中的一個問題說「遊戲結束」進行它保持關斷,我已經嘗試了很多東西和研究,但我似乎無法弄清楚,我只是一個初學者Python迷宮遊戲故障

`

進口時間 進口OS

print ("Your adventure starts as a young boy, running away from home becuase you're a rebel") 
    time.sleep(2) 
    print ("You find the famous labyrinth, do you go in?") 
    time.sleep(2) 
    answer = input("Make your choice, Yes OR No") 
    time.sleep(2) 
    print ("The answer",answer ,"got you stuck in a hole") 
    time.sleep(2) 
    print ("But you find a secret passage") 
    answer = input("Do you go through the door, Yes or No?") 
    if answer == "No": 
      time.sleep(2) 
      print ("Game Over.") 
    elif answer == "Yes": 
     time.sleep(2) 
     print("You hear a strange voice") 
     time.sleep(2) 
    answer = input("What do you say to the Voice, Hello or Who are you?") 
    if answer == "Hello": 
     print ("Hello") 
    elif answer == "Who are you?": 
     print ("Im your worst nightmare") 
    time.sleep(2) 
    print("You try and escape the labyrinth and turn a large gate with a gnome on the over end") 
    answer = input("Do you open the gate, Yes Or No?") 
    if answer == "Yes": 
     time.sleep(3) 
     print ("Game Over, you get brutally killed by a gnome, good job") 
     os._exit(0) 
    elif answer == "No": 
     time.sleep(3) 
     print ("You go the other way and see a light at the end of the tunnel") 
    answer = input("You see your family outside crying and waiting for you, do you go with them?") 
    if answer == "Yes": 
     print("You have a nice ending and you're sorry you ran away") 
     print("You have been graded: ") 
    elif answer == "No": 
     print("God smites you for being stupid.") 

     os._exit(0)` 
+1

如果對不起代碼接下來會發生什麼太混亂了,希望你有時間閱讀它。 – AGAC

+1

你可以在組織上使用一些工作。使用空格(如果需要,過分)。 – Jerrybibo

+0

完全同意兄弟,是在忙着做到這一點,沒有太多時間,只是一個愛好lol – AGAC

回答

2

取這個塊,例如

print ("But you find a secret passage") 
answer = input("Do you go through the door, Yes or No?") 
if answer == "No": 
     time.sleep(2) 
     print ("Game Over.") 
elif answer == "Yes": 
    time.sleep(2) 
    print("You hear a strange voice") 
    time.sleep(2) 
# continuation 

如果用戶輸入「否」,它將打印「遊戲結束」 - 我認爲這是正確的。但是,程序中的控制流繼續超過if/else塊。你需要做的是退出使用的東西的節目像sys.exit()或者確保您的控制流只有路徑前進,如果它應即包裹在if/else塊的truthy部分

if answer == "No": 
     time.sleep(2) 
     print ("Game Over.") 
elif answer == "Yes": 
    time.sleep(2) 
    print("You hear a strange voice") 
    time.sleep(2) 

    # put continuation here 
+2

或者,也可以在'print(「遊戲結束。」)''後面添加'return'語句) – alfasin

+0

謝謝,返回是一個好主意,現在測試它 – AGAC

+0

工程,很棒的工作。我很愚蠢 – AGAC