2016-08-17 50 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了解更多。

相关问题