2016-05-12 76 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