1

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()
    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()
    ...

1 Answers1

2

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)
        time.sleep(2)
        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.

Community
  • 1
  • 1
Kevin London
  • 4,628
  • 1
  • 21
  • 29