2017-02-24 82 views
0

我正在通過「學習python hardway」。我很抱歉,如果這是重複的,但我真的不知道我做錯了什麼。當我輸入小於50的數字時,它告訴我再次嘗試,並在第二次調用另一個函數中的字符串。如果我在第二個條目中輸入大於50的同一個東西,它會在另一個函數中調用另一個字符串。它會從green_dragon()調用並打印出「兩條龍都活着烹飪你並且吃掉你。」感謝您提供的任何見解。我爲簡單而道歉,哈哈。不得不讓我自己「的遊戲,我不認爲創意呢,哈哈。如果返回錯誤的答案(python)

def gold_room(): 
    print "You have entered a large room filled with gold." 
    print "How many pieces of this gold are you going to try and take?" 
    choice = raw_input("> ") 
    how_much = int(choice) 
    too_much = False 
    while True: 
     if how_much <= 50 and too_much: 
      print "Nice! you're not too greedy!" 
      print "Enjoy the gold you lucky S.O.B!" 
      exit("Bye!") 

     elif how_much > 50 and not too_much: 
      print "You greedy MFKR!" 
      print "Angered by your greed," 
      print "the dragons roar and scare you into taking less." 

     else: 
      print "Try again!" 
     return how_much 

def green_dragon(): 
    print "You approach the green dragon." 
    print "It looks at you warily." 
    print "What do you do?" 
    wrong_ans = False 
    while True: 
     choice = raw_input("> ") 

     if choice == "yell at dragon" and wrong_ans: 
      dead("The Green Dragon incinerates you with it's fire breath!") 
     elif choice == "approach slowly" and not wrong_ans: 
      print "It shows you its chained collar." 
     elif choice == "remove collar"and not wrong_ans: 
      print "The dragon thanks you by showing you into a new room." 
      gold_room() 
     else: 
      print "Both dragons cook you alive and eat you." 
      exit() 
+0

你永遠不會改變'too_much',所以第一個'if'永遠不會成功。 – Barmar

+0

在第二個函數中,你永遠不會改變'wrong_ans',所以第一個'if'永遠不會成功。 – Barmar

+0

這兩個變量有什麼意義? – Barmar

回答

1
too_much = False 

if <= 50 and too_much 

如果too_much設置爲False,爲什麼你所期望的,如果要計算的表達式爲true?它永遠不會去if裏面內環
移動用戶輸入以及

編輯:。

要停止while循環:

 too_much = True 
     while too_much: 
      choice = raw_input("> ") 
      how_much = int(choice) 
      if how_much <= 50: 
       print "Nice! you're not too greedy!" 
       print "Enjoy the gold you lucky S.O.B!" 
       too_much = False 
      else: 
       print "You greedy MFKR!" 
       print "Angered by your greed," 
       print "the dragons roar and scare you into taking less." 
+0

我將它改爲True它仍然做同樣的事情,但現在無法打印出來的字符串。 –

+0

我試圖在這些課程中將自己的文字遊戲作爲練習的一部分 –

+0

沒有它,它會在一次運行後停止。我試圖讓它一直要求輸入,直到它小於或等於50 –