Untitled
unknown
plain_text
18 days ago
1.5 kB
1
Indexable
Never
import random def generate_random_number(): random_number = random.randint(1, 100) return random_number def guess_number_game(): x = 0 random_number = generate_random_number() while x < 10: try: user_entry = int(input("Guess the number from 1 to 100:")) if user_entry == random_number or random_number < user_entry < 101 or 0 < user_entry < random_number: x = x + 1 if user_entry == random_number: print(f"Good job! Number of guesses: {x}") break elif random_number < user_entry < 101: print("Overshot the mark!") elif 0 < user_entry < random_number: print("Undershot the mark!") elif 1 > user_entry or user_entry > 100: print("Invalid Input! Please input a number from 1 to 100!") if x == 10: print("Sorry, no more guesses!") print(f"Random number was: {random_number}") new_game() except ValueError: print("Invalid Input! Please input a number from 1 to 100!") def new_game(): new_game_prompt = input("Would you like to start a new game? y/n ") new_game_prompt = new_game_prompt.strip() match new_game_prompt: case "y": guess_number_game() case "n": print("Game Over") if __name__ == "__main__": guess_number_game()
Leave a Comment