Python 我的二分法搜索算法错了吗?
如果我问了一个愚蠢的问题,我很抱歉,但是我有点困惑。。。 我一直在edx学习MIT6.00X课程,其中一个练习是使用二分法搜索算法来查找密码。我花了大约4个小时完成了这个练习(是的,我是一个noob),但我成功地构建了以下代码:Python 我的二分法搜索算法错了吗?,python,algorithm,Python,Algorithm,如果我问了一个愚蠢的问题,我很抱歉,但是我有点困惑。。。 我一直在edx学习MIT6.00X课程,其中一个练习是使用二分法搜索算法来查找密码。我花了大约4个小时完成了这个练习(是的,我是一个noob),但我成功地构建了以下代码: numGuesses = 0 lo = 0 hi = 100 mid = (hi + lo)/2 num = raw_input( "Input a number between 0 and 100 ") if num > 0 or num < 100:
numGuesses = 0
lo = 0
hi = 100
mid = (hi + lo)/2
num = raw_input( "Input a number between 0 and 100 ")
if num > 0 or num < 100:
while mid != num:
print ("Is your number " + str(mid) + "?")
userinput = raw_input( "Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if userinput == 'h':
hi = mid
mid = (hi + lo)/2
elif userinput == 'l':
lo = mid
mid = (hi + lo)/2
elif userinput == 'c':
print ("Game over. Your secret number was:" + str(mid))
break
else:
print ("Sorry, I did not understand your input.")
else:
print ("You should use a number between 0 and 100")
numGuesses=0
lo=0
hi=100
中=(高+低)/2
num=原始输入(“输入一个介于0和100之间的数字”)
如果num>0或num<100:
中途!=号码:
打印(“是您的号码”+str(mid)+“?”)
userinput=raw_input(“输入'h'表示猜测过高。输入'l'表示猜测过低。输入'c'表示我猜测正确。”)
如果userinput=='h':
高=中
中=(高+低)/2
elif userinput==“l”:
lo=中
中=(高+低)/2
elif userinput=='c':
打印(“游戏结束。你的密码是:“+str(mid))
打破
其他:
打印(“对不起,我不理解您的输入。”)
其他:
打印(“应使用介于0和100之间的数字”)
虽然手工测试效果很好,但在练习中有一些问题没有通过,主要是因为网站没有一直猜测它是高还是低,有时它按错键,我没有通过练习
在尝试更改代码后,我无法完成课程,因此我看到了答案,如果我做错了,我应该使用布尔值来保持代码流动,直到找到正确的数字
我的问题是:我的代码错了吗?还有,我是否犯了任何错误,妨碍了网站按正确的字母?只是好奇
非常感谢这是我今天终于解决的一个MITx手指练习。以下是我的方法:
lo = 0
hi = 100
mid = (hi + lo)/2
print 'Please think of a number between 0 and 100!'
while True:
print ("Is your number " + str(mid) + "?")
userinput = raw_input( "Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if userinput == 'h':
hi = mid
mid = (hi + lo)/2
elif userinput == 'l':
lo = mid
mid = (hi + lo)/2
elif userinput == 'c':
print ("Game over. Your secret number was:" + str(mid))
break
else:
print ("Sorry, I did not understand your input.")
print('Please think of an integers BETWEEN 0 and 100!')
#Define variable
x=100
low=0
high=x
ans=0
#Guessing code part
while ans<=x:
print'Is your secret number:', str((low+high)/2), '?'
s=raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly:")
if s!='h' and s!='l' and s!='c':
print'Sorry I did not understand your input.'
elif s=='h':
high=(low+high)/2
elif s=='l':
low=(low+high)/2
elif s=='c':
print'Game over. Your secret number is:', str((low+high)/2)
break
print('请考虑一个介于0和100之间的整数!')
#定义变量
x=100
低=0
高=x
ans=0
#猜测代码部分
我很困惑。什么是“按错键”的“站点”?这个脚本的用户是您也创建的名为“Site”的机器人吗?您想知道如何让您的脚本绕过用户机器人中的bug?(如果是,答案取决于你在机器人中内置了多少激光器。)你的If语句可以接受大于100小于0的值。你说的是“如果val>0或val<100”。。。如果val=300,则它大于零但不小于100。。。既然你有一个手术室。。。它仍将评估为真。使用和。此外,如果您试图为输入不可能的内容的用户提供更好的错误处理(例如,您知道hi==mid==low==49,您问49,他们说“h”),您可以在if
/elif/
elif`code中检查,并拒绝不可能的答案,或者在循环的顶部进行检查,一旦遇到不可能的情况就退出。此外,如果不允许0或100,那么说“输入一个介于0和100之间的数字”也有点奇怪。程序员希望0是有效的,但不是100。普通用户可能会认为这两种方法都是有效的。如果删除该复选框,其余代码可以成功猜到0,但不能猜到100。@abarnert我正在edx.org上学习介绍课程,在发送代码后的练习中,站点使用机器人选择一些数字并测试代码,该机器人选择数字“8”例如,当猜测值太高时,它一直按h键,然后突然随机按l键。我建议在循环的顶部设置类似middle=(low+high)/2
,这样你就不必重复多次该表达式了。请尝试解释你的答案!