2016-08-17 15 views
1

我正在研究一個簡單的基於文本的瑣事遊戲作爲我的第一個python項目,並且我的程序一旦達到分數限制就不會終止。當達到分數限制時程序沒有終止?

def game(quest_list): 
    points = 0 
    score_limit = 20 

    x, y = info() 
    time.sleep(2) 

    if y >= 18 and y < 100: 
     time.sleep(1) 
     while points < score_limit: 
      random.choice(quest_list)(points) 
      time.sleep(2) 
      print("Current score:", points, "points") 
     print("You beat the game!") 
     quit() 
    ... 
+6

'points'永遠不會增加,因此循環將永遠不會終止 – FujiApple

回答

2

看起來像points變量沒有增加。像這樣的東西可能會在你的內循環工作:

while points < score_limit: 
     points = random.choice(quest_list)(points) 
     time.sleep(2) 
     print("Current score:", points, "points") 

我假設quest_list是函數的列表,你傳遞的points值作爲參數?爲了使這個例子有效,你還需要返回quest_list返回的函數中的點。一個可能更簡潔的方式來建立這個將只返回任務產生的點。然後,你可以這樣做:

 quest = random.choice(quest_list) 
     points += quest() 

除非points是一個可變的數據結構,也不會改變的價值。你可以在this StackOverflow question瞭解更多。

相關問題