Final answer:
The high-low game using a while loop:
import random
actual_number = random.randint(1, 500)
while True:
guess = int(input('Guess a number between 1 and 500: '))
if guess == actual_number:
print('Congratulations! You guessed the correct number!')
break
elif guess < actual_number:
print('Too low! Try again.')
else:
print('Too high! Try again.')
Step-by-step explanation:
To recreate the high-low game using a while loop, you can follow these steps:
- Prompt the user to input a number between 1 and 500.
- Use a while loop to repeatedly ask the user to guess a number until they guess correctly.
- Inside the loop, compare the user's guess with the actual number and provide feedback on whether the guess is too high or too low.
- If the user guesses correctly, display a message indicating that they won.
Here's the code:
import random
actual_number = random.randint(1, 500)
while True:
guess = int(input('Guess a number between 1 and 500: '))
if guess == actual_number:
print('Congratulations! You guessed the correct number!')
break
elif guess < actual_number:
print('Too low! Try again.')
else:
print('Too high! Try again.')