2013-03-11 22 views
-7
def Game(): 

    # Story Background 
    print "You decide to take a walk outside one night when you come accross a corn field." 
    print "You notice an omnious sound coming from the other side of the maze." 
    Enter = raw_input("Do you enter? (yes or no)") 

    if Enter == "Yes" or "yes": 
     print "You walk into the maze and the corn is so thick together you cant push through" 
     print "so you walk down the isle surrounded by corn and you come to an intersection." 
     turn = raw_input("Which way do you go? (Left, Right, Forward, Leave)") 

     if turn == "Left" or "left": 
      print "After you turn left you come accross a dead end and you are forced to turn around." 
      print "You return to the intersection." 
      turn2 = raw_input("Which way do you go? (Left, Forward, Leave)") 

      if turn2 == "Forward" or "forward": 
       print "you walk on deeper into the maze when you come to a left turn" 
       print "you turn left and come accross a crossroad." 
       turn3 = raw_input("Which way do you go? (Left, Right, Leave)") 

       if turn3 == "Right" or "right": 
        print "You come to a dead end and are forced to turn around" 
        turn4 = raw_input("Which way do you go? (Forward, Leave)") 

        if turn4 == "Forward" or "forward": 
         print "You walk to a hole in the ground stopping you from moving any further" 
         print "the hole seems to be filled with snakes so you cant go through it." 
         print "you are forced to leave the maze." 

        elif turn4 == "leave" or "Leave": 
         print "you leave the maze and return home." 

        else: 
         print "you walk into a wall and go into an irreversable coma" 

       elif turn3 == "Left" or "left": 
        print "You walk to a hole in the ground stopping you from moving any further" 
        print "the hole seems to be filled with snakes so you cant go through it." 
        print "you are forced to leave the maze." 
        print "you leave the maze and return home." 

       else: 
        print "you walk into a wall and go into an irreversable coma" 

      elif turn2 == "Left" or "left": 
       print "you turn Left into the maze when you come by a strange man laying on the ground." 
       man == raw_input("What do you do? (Help, keep going)") 

       if man == "Help" or "help": 
        print "you help the man up and he knocks you out cold" 
        print "you wake back up in your bed at home" 

       elif man == "keep going" or "Keep going": 
        print "You leave the man behing after stealing his wallet." 
        print "YOU HAVE REACHED THE END OF THE MAZE" 
        print "You realize the noise was the sound of a old farmer milking a cow." 
        print "The farmer nags at you for coming on private property." 

       else: 
        print "you walk into a wall and go into an irreversable coma" 

      elif turn2 == "leave" or "Leave": 
       print "you leave the maze and return home." 

      else: 
       print "you walk into a wall and go into an irreversable coma" 

     elif turn == "Forward" or "forward": 
      print "you move forward into the maze when you come by a strange man laying on the ground." 
      man == raw_input("What do you do? (Help, keep going)") 

      if man == "Help" or "help": 
        print "you help the man up and he knocks you out cold" 
        print "you wake back up in your bed at home" 

      elif man == "keep going" or "Keep going": 
        print "You leave the man behing after stealing his wallet." 
        print "YOU HAVE REACHED THE END OF THE MAZE" 
        print "You realize the noise was the sound of a old farmer milking a cow." 
        print "The farmer nags at you for coming on private property." 

      elif turn == "leave" or "Leave": 
        print "you leave the maze and return home." 

      else: 
       print "you walk into a wall and go into an irreversable coma" 

     elif turn == "Right" or "right": 
      print "After you turn right you come into a left turn only path." 
      print "You turn left and you come to a crossroad." 
      turn = raw_input("Which way do you go? (Left, Right, Leave)") 

      if turn == "Right" or "right": 
       print "You come to a dead end and are forced to turn around" 
       turn = raw_input("Which way do you go? (Forward, Leave)") 

       if turn == "Forward" or "forward": 
        print "You walk to a hole in the ground stopping you from moving any further" 
        print "the hole seems to be filled with snakes so you cant go through it." 
        print "you are forced to leave the maze." 

       else: 
        print "you walk into a wall and go into an irreversable coma" 

      elif turn == "Forward" or "forward": 
       print "You walk to a hole in the ground stopping you from moving any further" 
       print "the hole seems to be filled with snakes so you cant go through it." 
       print "you are forced to leave the maze." 

      else: 
       print "you walk into a wall and go into an irreversable coma" 

     elif turn == "Leave" or "leave": 
      print "you leave the maze and return home." 

     else: 
      print "you walk into a wall and go into an irreversable coma" 



    elif Enter == "No" or "no": 
     print "You walk on into the depths of the night and are robbed by a couple street thugs." 

    else: 
     print "you walk into a wall and go into an irreversable coma" 






def main(): 

    Game() 

main() 

當我使用這個程序,不管我進入蟒蛇的外殼,它說同樣的事情一遍又一遍..它不會採取報表的raw_input的來龍去脈,並把它們進入if語句Python程序錯誤的elif否則,如果

+2

哇...這是一個'如果'樹的地獄。有沒有考慮過使用表格? – nneonneo 2013-03-11 00:54:38

+3

而且*它說了什麼? – wRAR 2013-03-11 00:54:42

+0

當然你不會得到這個問題的答案..你應該在發佈前的指導原則。 – Azi 2013-03-11 00:56:55

回答

2
if Enter == "Yes" or "yes": 

這不是如何or作品。這被解釋爲if (Enter == "Yes") or "yes":。在python中,非空字符串總是如此,因此所有的if這樣的語句都是正確的。我會建議像

if Enter.lower() == "yes": 

它照顧所有的大/小寫的組合。

+0

謝謝,你的建議在某種程度上有所幫助。我刪除了所有的或它的工作。它有一些錯誤,但不像以前那樣 – 2013-03-11 01:14:18

+2

編寫這樣的程序是一個巨大的bug。 – Matthias 2013-03-11 01:47:18

+0

@Matthias同意。我會建議一個簡單的狀態機。 – 2013-03-11 07:01:38

1

你的語法是錯在這裏:

if Enter == "Yes" or "yes": 

這個條件總是爲真。首先評估Enter == "Yes"。如果布爾表示是False,則將考慮"yes"的布爾表示。 bool("yes")始終爲True

考慮做類似:

if Enter in ('Yes', 'yes'): 

或者:

if Enter.lower() == 'yes':