猜数字游戏Python

时间:2020-04-26 08:32:28

标签: python math

我想写一个猜用户的数字猜游戏号码。即使给出正确的答案,循环也不会结束。我该怎么办?

    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

1 个答案:

答案 0 :(得分:0)

您应将break从末端移到if ans=="b":