MIT 6.00.1x 第三讲
主要内容包括:
· Iteration loops
· “Guess & Check” Algorithms
· Exhaustive enumeration
· Floating point accuracy
· Bisection Search
· Newton-Raphson Algorithm
ProblemSet:In this problem, you’ll create a program that guesses a secret number!
x = int( raw_input('Enter an integer between 0 and 100:'))
numGuesses = 0
low = 0
high = 100
ans = (high + low) / 2
while abs( ans - x ) >= 0:
print ('Is your secret number '+str(ans)+'?')
y = str( 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 y == 'h':
high = ans
elif y == 'l':
low = ans
elif y == 'c':
print('Game over. Your secret number was: '+ str(ans))
break
elif y =='quit':
break
else:
print('Sorry, I did not understand your input.')
ans = (high + low) / 2
我的代码在本机上跑起来没问题,在线提交之后出现了错误,应该是由于他的输入设置和我的未能匹配的缘故,暂时还不知道原因,有待寻找。
另外在写代码的过程中遇到的问题是:
当用户输入不符合判断的字符时,要如何跳转回循环中最初的语句,粗略的查了一下之后似乎python中不支持goto等功能,有待研究。