Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

from random import randint

while True:
choice = input("Choose a game mode (U for user plays, M for machine plays, Q to
quit): ")

if choice == 'U':
number = randint(1, 999)
user_tries = 0

while True:
guess = int(input("Guess a number between 1 and 999: "))
user_tries += 1

if guess < number:


print("The number is bigger.")
elif guess > number_to_guess:
print("The number is smaller.")
else:
print(f"Congratulations! You found the number {number} in
{user_tries} tries.")
break

elif choice == 'M':


user_number = int(input("Choose a number between 1 and 999 for the machine
to guess: "))
lower_limit, upper_limit = 1, 999
machine_tries = 0

while True:
machine_guess = randint(lower_limit, upper_limit)
machine_tries += 1
response = input(f"Is {machine_guess} too small (S/s), too large (B/b),
or found (F/f)? ")

if response in ['F', 'f']:


print(f"The machine found the number {user_number} in
{machine_tries} tries.")
break
elif response in ['S', 's']:
lower_limit = machine_guess + 1
elif response in ['B', 'b']:
upper_limit = machine_guess - 1

elif choice == 'Q':


break
else:
print("Invalid choice. Please choose 'U', 'M', or 'Q'.")

You might also like