0% found this document useful (0 votes)
2 views

random number

The document outlines a Python program that allows users to guess a randomly generated number within a specified range. It includes steps for input validation, adjusting the guessing range based on user input, and providing feedback until the correct number is guessed. The program also handles invalid inputs gracefully by prompting the user to enter valid integers.

Uploaded by

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

random number

The document outlines a Python program that allows users to guess a randomly generated number within a specified range. It includes steps for input validation, adjusting the guessing range based on user input, and providing feedback until the correct number is guessed. The program also handles invalid inputs gracefully by prompting the user to enter valid integers.

Uploaded by

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

'''

Write a Python program that allows the user to guess a randomly


generated number within a specified range. The program should:

1. Prompt the user to enter a lower and an upper bound. If the lower
bound is greater than the upper bound, swap them to ensure a valid
range.

2. Generate a random number within the given range.

3. Continuously prompt the user to guess the number.

4. If the guess is incorrect, update the range accordingly:

- If the guess is too low, adjust the lower bound to be one more than the
guess.

- If the guess is too high, adjust the upper bound to be one less than the
guess.

5. Display the updated range after each incorrect guess.

6. End the game when the user guesses the correct number and display a
congratulatory message.

7. Handle invalid inputs gracefully by prompting the user to enter a valid


integer.

'''

import random

# get user input for the range

while True:

try:

lower = int(input("Enter the lower bound: "))

upper = int(input("Enter the upper bound: "))

if lower > upper:

lower, upper = upper, lower # swap if user enters the wrong order
for lower and upper

break
except ValueError:

print("Invalid input. Please enter an integer!")

# generate a random number within the range

number = random.randint(lower, upper) #number = random.randint(0,


100)

while True:

try:

guess = int(input(f"Guess a number between {lower} and {upper}:


"))

if guess == number:

print(f"Congratulations! You guessed the correct number:


{number}")

break

elif guess <= lower or guess >= upper:

print(f"Your number is out of range! Please enter a number


between {lower} and {upper}: ")

elif guess < number:

lower = guess

print(f"Sorry! You guessed the wrong number and the new range is
between {lower} and {upper}")

else:

upper = guess

print(f"Sorry! You guessed the wrong number and the new range is
between {lower} and {upper}")

except ValueError:

print("Invalid input. Please enter an integer!")

You might also like