Python while循环多次询问输入

2024-10-03 11:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图通过猜数字程序来解决这个问题, 程序必须打印Ans的第一个数字是(STARTOW+STARTHEGH)/2,然后Ans根据输入进行更新

我不明白为什么我的while循环一直等待输入至少2次,直到它打印结果为止,即使我按l或h(除非我按c),这会中断循环

Startlow = 0
Starthigh = 100
Ans = (Startlow + Starthigh)/2
print("Please think of a number between 0 and 100!")

while True:
    print("Is your secret number " + str(int(Ans)))
    if input() == "c":
        print("Game over,Your secret number was: "+str(int(Ans)))
        break
    elif input() == "l":
        Startlow = Ans
        Ans = (Startlow + Starthigh)/2
    elif input() == "h":
        Starthigh = Ans
        Ans = (Startlow + Starthigh)/2
    else:
        print("Sorry, I did not understand your input.")

感谢您的帮助:)


Tags: 程序numberinputyoursecret数字intprint
2条回答

只要做:

x = input()
if x == "c":
  #And so on...

您应该在循环中请求输入一次,然后将该答案与您想要的项目进行比较

相反,您在每个条件下都请求一个(可能不同的)答案

所问问题的数量取决于你遇到了多少条件

相关问题 更多 >