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

Python

The document describes a random number guessing game that allows a user to guess a randomly generated number within a set range. The program imports a random number module, prompts the user to enter upper and lower limits, randomly selects a number within that range, and allows the user up to 8 guesses to match the random number. With each guess, the program provides feedback if the number is too high or low and reveals the number if the user runs out of guesses.

Uploaded by

Hema Tharun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Python

The document describes a random number guessing game that allows a user to guess a randomly generated number within a set range. The program imports a random number module, prompts the user to enter upper and lower limits, randomly selects a number within that range, and allows the user up to 8 guesses to match the random number. With each guess, the program provides feedback if the number is too high or low and reveals the number if the user runs out of guesses.

Uploaded by

Hema Tharun
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

while True:

# Import random module.


import random

# Choose an upper limit and a lower limit.


# Use the int function to ensure the value entered is an integer.
lower_limit = int(input("Enter the lower limit: "))
upper_limit = int(input("Enter upper limit: "))

# We select a number randomly and store it in variable


# The function takes the upper limit and the lower limit as parameters and
picks a number between the two numbers.
# In python, variables can be declared and assigned at the same time
random_number = random.randint(lower_limit, upper_limit)
print("You will have to choose a number between ", upper_limit, " and ",
lower_limit)

# We assign a variable "Chances" that will act as the counter for a loop
# The user will have to input his guess so we assign his guess into a variable.
chances = 0
while chances < 8:
chances += 1
guess = int(input("Enter your guess: "))
if random_number == guess:
print("Congragulations, you did it. The number was ", random_number)
break
elif guess < random_number:
print("You guessed a small number.")
elif guess > random_number:
print("You guessed a large number.")
if chances == 7:
print("\n You've run out of chances")
print("\n The number was ", random_number)
print("Better luck next time")
break
print("\n")
break

You might also like