学习python,目前正在学习二分法解题。我正在编写代码,该代码应该接受用户从 0 到 100 的猜测,并尝试使用二分法找到该猜测。这是代码:
answer = raw_input('Please think of a number between 0 and 100')
#I've been using 80 as my test case
low = 0
high = 100
guess = (low+high)/2
while guess != answer:
if guess < answer:
low = guess
else:
high = guess
guess = (low+high)/2
我意识到,当我的猜测 < 答案为 false 时,else 块不会执行,所以我的高数永远不会改变。为什么会这样?我在这里忽略了什么吗?