Repeat game again

Here’s my code:

I’ve spent around 5 hours today working on how to add a repeat feature (e.g "Play again? y/n).
However, everything that I’ve tried hasn’t worked. Also my code in general could use some improvements as well. Help would be GREATLY appreciated.

while True:
    play_game()
    ask user whether finished
    if finished:
        break

…?

3 Likes

An unfinished pseudo-code placeholder, maybe?

I wrote it, it’s not from their code.


Also, regarding general improvements, there’s a lot of repeated code around difficulty, so maybe put descriptions of difficulties somewhere, and then refer to that?

difficulties = [
    Difficulty(
        description="Easy",
        lives=9,
        defeat_msg="^ Try again - You can do it ^",
        victory_msg="^ Have a go at Medium difficulty next time ^",
    ),
    ...
]
...
lives = difficulty.lives
...
if correct == 20:
    ...
    print(difficulty.victory_msg)

Whenever something visually looks repeated, then, whatever logic went into writing that repeated code could go directly into the code instead, like storing data in lists or other data structures and then looking up the appropriate one or iterating through them all or whatever the situation calls for.

3 Likes

Ha, fair enough. :slight_smile:

2 Likes