18.5k views
1 vote
Using a while loop, re-create the high-low game by asking a user to input a number between 1 and 500.

1 Answer

6 votes

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:

  1. Prompt the user to input a number between 1 and 500.
  2. Use a while loop to repeatedly ask the user to guess a number until they guess correctly.
  3. 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.
  4. 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.')
User Kiskae
by
9.1k points