大家好我刚开始使用python编程。 我写了这个通用代码,它会保持二等分直到找到正确的数字。但它不起作用。代码一直在块内盘旋“如果猜测
num=raw_input("choose a number between 0-100 ")
l=0 #lower limit
u=100 #upper limit
guess=(l+u)/2
print num
print "is it =",guess
while guess!=num:
if guess==num:
break
if guess<num:
l=guess
guess=(l+u)/2
print "guessed low,new guess = ",guess
elif guess>num:
u=guess
guess=(u+l)/2
print "guessed high,new guess = ",guess
答案 0 :(得分:2)
您的条件不起作用,因为num
是一个字符串。首先将其转换为整数,如下所示:
num = int(raw_input("choose a number between 0-100 "))