0

大家好,我刚开始使用 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
4

1 回答 1

2

你的条件不起作用,因为num是一个字符串。首先将其转换为整数,如下所示:

num = int(raw_input("choose a number between 0-100 "))
于 2013-03-22T12:07:37.990 回答