vendredi 4 mars 2022

How can I stop my game from generating a new number while it's running?

I'm a beginner python developer. I've been experimenting with the random module and decided to create a guessing game. Problem is, Whenever the user inputs a guess, a new number is generated. You can find this in the "run" loop. Here's the code.

import random
from time import sleep

print("""                                        Number Guessing Game
        Type 'Quit' To Exit The Game
Choose a Level:
    (1)Easy
    (2)Medium
    (3)Hard
""")

run = True

def play_game():    
    play_game.choice = input("> ")

    print("\nGuess The Number!\n")

    return play_game.choice

play_game()

while run:
    if play_game.choice == "1":
        r = random.randint(1, 10)

        print(r)

        guess = int(input("> "))

        if guess == r:
            print("You Won!")

            break

        elif guess > r:
            print("Too High!\nGuess Again.")

        elif guess < r:
            print("Too Low!\nGuess Again.")

    elif play_game.choice == "2":
        r = random.randint(1, 50)
            
        print("\nGuess The Number!")

        if r == guess:
            print("You Won!")

            break

        elif guess > r:
            print("Too High!\nGuess Again.")

        elif guess < r:
            print("Too Low!\nGuess Again.")

    elif play_game.choice == "3":
        r = random.randint(1, 100)

        print("\nGuess The Number!\n")

        if r == guess:
            print("You Won!")

            break

        elif guess > r:
            print("Too High!\nGuess Again.")

        elif guess < r:
            print("Too Low!\nGuess Again.")

    elif play_game.choice.upper() == "QUIT":
        print("See You Later!")

        sleep(1)

        break

    else:
        print("Unknown Command!\nTry Again")

Could you please help me figure out what's wrong? Thanks!




Aucun commentaire:

Enregistrer un commentaire