Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a game where Python uses the randrange() function to generate a number be

ID: 3821088 • Letter: C

Question

Create a game where Python uses the randrange() function to generate a number between 1 and 100. Set up a loop to ask the user to guess the number. Your response after each guess will be either “lower” or “higher” to indicate if the guess is too low or too high. The game stops when the user guesses the correct number.

This is NOT the answers: import random st=1 ed=100 while True: n = random.randrange(st,ed+1) print("The guess is "+str(n)) print("1) The number is too high.") print("2) The number is too low.") print("3) The number is right.") inp = input("Select an option:") if(inp==1): ed = n-1 elif(inp==2): st = n+1 else: break

Explanation / Answer

# pastebin link of code: https://pastebin.com/jNCvjDxf

import random
st=1
ed=100
guess = 0
n = random.randrange(st,ed+1)
while True:
guess = int(input("Enter a number between 1 to 100 to guess my number: "))
if guess < n:
print("lower")
elif guess > n:
print("higher")
else:
print("you have guessed correctly")
break

'''
Sample output
Enter a number between 1 to 100 to guess my number: 20
higher
Enter a number between 1 to 100 to guess my number: 5
lower
Enter a number between 1 to 100 to guess my number: 10
higher
Enter a number between 1 to 100 to guess my number: 7
lower
Enter a number between 1 to 100 to guess my number: 8
lower
Enter a number between 1 to 100 to guess my number: 9
you have guessed correctly
'''

Please rate positively if this answered your query.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote