2015-07-19 119 views
1

我正在研究一個基本的文本遊戲,以提高我的python技能。我有一個部分,有一系列的if語句,我想用while循環來防止人們在給定的3之外使用選擇,然後它將循環回到開始並再次詢問它們。Python中基本while循環的問題

由於某些原因,它打印'不計算請再試一次',即使正確的選擇1-3給出,我不明白爲什麼。我一直在關注如何做到這一點,我所做的一切似乎是相同結構,因此不知道林做錯了一節課....

def enter(self): 
    print "You move down into the east tunnel" 
    print "You walk down the tunnel for what seems like a long time, there are flickering torches along the wall that give off small amounts of light but its still" 
    print "very difficult to see anything ahead" 
    print "You hear a growling and shuffling noise ahead.....as you peer into the gloom you see a large troll!" 
    print "what will you do?" 

print """1 - draw your sword and fight him. 
     2 - try to run past him. 
     3 - use one of your objects. 
     """ 

troll_choice = raw_input("> ") 

while troll_choice != "1" or troll_choice != "2" or troll_choice != "3": 
    print "doesnt compute please try again" 

    troll_choice = raw_input("> ") 

if troll_choice == "1": 
    print "you will fight the troll" 
elif troll_choice == "2": 
    print "The troll looks slow and stupid but not as slow as you think! As you try to rush past him he brings his giant club down on top of your head" 
    print "killing you instantly" 
    return 'death' 

elif troll_choice == "3": 
    print "which of your items will you use?" 
    items_choice = raw_input("> ") 
    if items_choice == "matches": 
      print "You fool, matches are useless against a creature of this size, he slays you swiftly" 
      return 'death' 

    elif items_choice == "gold coin": 
      print "you flick the gold coin at the Troll, the troll looks puzzled and watches the shiny coin arch through the air, no is your chance to attack!" 
      print "the Troll's defences are down, you draw your sword and lunge towards its soft underbelly and drive your sword home killing it instantly" 
      print "you wipe the blood from your sword and head on down the tunnel" 

      return 'room_b' 

    elif items_choice == "flashbang dust" or items_choice == "flashbang": 
      print "You pull out the puch of flashbang dust and throw it hard on the floor at the trolls feet while shielding your eyes..." 
      print "the blast and white light from the explosion blinds the troll, it roars and covers its eyes..." 
      print "you decide to use this time to get past the troll and sprint down the tunnel leaving him behind" 

      return 'room_b' 

    elif items_choice == "silver dagger" or items_choice == "iron cross": 
      print "Why would these be of any use against a troll??" 
      print "he quickly slays you!" 

      return 'death' 
+0

將條件更改爲'troll_choice!=「1」和troll_choice!=「2」和troll_choice!=「3」'。其目前正在評估,如果它發生不等於其中任何一個.. – amdixon

回答

2

while循環繼續循環,直到條件爲假,條件你給 -

while troll_choice != "1" or troll_choice != "2" or troll_choice != "3": 

永遠不會成爲假的,因爲troll_choice不能1以及2以及3在同一時間。所以它會無限期地繼續看。

要在條件中使用and而不是or,以在troll_choice變爲三者之一時打破循環。

但更好的方法是使用in算子的 -

while troll_choice not in ["1" , "2" , "3"]: 
+0

非常感謝,我將這標記爲答案在一分鐘 –

0

我很抱歉,我不能成功地運行您的程序,也許有些不妥。

我只是給一個簡單的解決方案。

def loop(): 
while 1: 
    print "please input 1,2 or 3" 
    num = int(raw_input("> ")) 
    if num < 1 or num > 3: 
     print "please try again" 
     continue 
    return 

loop() 

您可以根據需要使用更改。