Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
1
Indexable
Never
# Import the random module
import random


def generate_random_number():
    # Generate a random number between 1 and 100
    random_number = random.randint(1, 100)
    return random_number


def guess_number_game():
    random_number = generate_random_number()
    guesses = 0
    while guesses < 10 and True:
        try:
            user_input = int(input("Input a random number between 1 and 100: "))
            if user_input < 0:
                print("Please enter a number between 0 and 100")
                guesses -= 1
            elif user_input > 100:
                print("Please enter a number between 0 and 100")
                guesses -= 1
            elif user_input == random_number:
                print(f"You have guessed the correct number and it was {random_number}")
                break
            elif user_input > random_number:
                print("Your guess is too high")
            elif user_input < random_number:
                print("Your guess is too low")

            guesses += 1
            print(f"You have used {guesses} guesses, and have {10 - guesses} left")
        except ValueError:
            print("Please input a number")
            if guesses == 0:
                print(f"You have {10 - guesses} guesses left")
            else:
                print(f"You have {10 - guesses} guesses left")
                continue
    else:
        print(f"You have used all your guesses. The right number was {random_number}")


if __name__ == '__main__':
    while True:
        user_action = input("Please enter Start, Continue or End game: ")
        if user_action.title().startswith("Start") or user_action.title().startswith("Continue"):
            guess_number_game()
            continue
        elif user_action.title().startswith("End"):
            break
    print("Ending the game, thanks for playing!")
Leave a Comment