0% found this document useful (0 votes)
14 views1 page

Ambiguity

The document contains a Python function called 'guess_the_number' that implements a number guessing game. The game generates a random number between 1 and 100 and prompts the user to guess it, providing feedback on whether the guess is too low, too high, or correct. It also handles invalid input by prompting the user to enter a valid integer.

Uploaded by

gg4480
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Ambiguity

The document contains a Python function called 'guess_the_number' that implements a number guessing game. The game generates a random number between 1 and 100 and prompts the user to guess it, providing feedback on whether the guess is too low, too high, or correct. It also handles invalid input by prompting the user to enter a valid integer.

Uploaded by

gg4480
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import random

def guess_the_number():
# Generate a random number between 1 and 100
number_to_guess = random.randint(1, 100)
attempts = 0
print("Welcome to Guess the Number!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")

while True:
try:
# User input for guess
user_guess = int(input("Enter your guess: "))
attempts += 1

if user_guess < number_to_guess:


print("Too low! Try again.")
elif user_guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Correct! You guessed the number in {attempts} attempts.")
break
except ValueError:
print("Please enter a valid integer.")

guess_the_number()

You might also like