| 4 | |
| 5 | # Function to play a guessing game where the user tries to guess the number chosen by the computer |
| 6 | def guess(): |
| 7 | # Generate a random number between 1 and 20 (inclusive) |
| 8 | n = random.randrange(1, 20) |
| 9 | |
| 10 | # Request user input to guess the number |
| 11 | var_guess = int(input("Enter any number: ")) |
| 12 | |
| 13 | # Continue the loop until the user guesses the correct number |
| 14 | while n != var_guess: |
| 15 | if var_guess < n: # If the user's guess is too low |
| 16 | print("OOPS! Too low") |
| 17 | var_guess = int(input("Guess again: ")) # Ask for another guess |
| 18 | elif var_guess > n: # If the user's guess is too high |
| 19 | print("OOPS! Too high!") |
| 20 | var_guess = int(input("Guess again: ")) # Ask for another guess |
| 21 | else: |
| 22 | break # Exit the loop when the correct number is guessed |
| 23 | |
| 24 | # Display a congratulatory message when the user guesses the correct number |
| 25 | print(f"Congratulations!! You guessed the number {n} correctly") |
| 26 | |
| 27 | |
| 28 | # Function for the computer to guess the number chosen by the user |