Ryan Dregney Ryan Dregney - 1 year ago 67
Python Question

Program not terminating when score limit is reached?

I'm working on a simple text-based trivia game as my first python project, and my program won't terminate once the score limit is reached.

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

x, y = info()

if y >= 18 and y < 100:
while points < score_limit:
print("Current score:", points, "points")
print("You beat the game!")

Answer Source

It looks like the points variable is not increased. Something like this might work in your inner loop:

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

I'm assuming that quest_list is a list of functions, and you're passing the points value as an argument? To make this example work, you'll also want to return the points from the function returned by the quest_list that's called. A perhaps cleaner way to build this would be to return only the points generated by the quest. Then you could do something like:

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

Unless points is a mutable data structure, it won't change the value. You can read more about that in this StackOverflow question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download