I want to write a number guessing game number taken from a user. Even though the right answer comes out the loop does not end. What should I do?
number=int(input("Enter a number between 1 and 100: "))
guess=50
n=1
if number>100:
number=int(input("Enter a number less than 100: "))
if number<1:
number=int(input("Enter a number greater than 1: "))
while True:
print("Your number is" +' '+ str(guess) +' '+ "?")
ans=str(input("(g)reater,(l)ess or (b)ravo: "))
for n in range(1,10):
if ans=="g":
guess=guess+guess//(2^n)
elif ans=="l":
guess=guess-guess//(2^n)
elif ans=="b":
print("Your number is " +' '+ str(guess) +' '+ "Well done for me")
break
You should move break
from end into if ans=="b":
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.