改写小助手
有位小 A 同学,学习了前一章的猜数游戏程序之后,决定自己把攻击者小助手重新实现一遍。
不过小 A 想:程序写出来是我自己用嘛,何必搞那么麻烦呢?于是去掉了一些限制。另外,最好每一轮都打印出当前是第几轮,这样不是看得更清楚吗?于是,程序变成了下面这样:
low = 1
high = 1000
loopNum = 0
while low <= high:
m = int((high - low) / 2) + low
loopNum += 1
print("[Loop %s]: My guess is %s" % (loopNum, m))
userInput = ""
while userInput != '1' and userInput != '2' \
and userInput != '3' :
print("\t\t 1) %s == sn \n\
2) %s < sn.\n\
3) %s > sn." % (m, m, m))
userInput = input("Your option:")
userInput = userInput.strip()
if userInput == '1':
print("Succeeded! SN is: ", m)
break
else:
if userInput == '2':
low = m
else:
high = m
if low > high:
print("Failed!")
然后小 A 同学自己试了一下——心里想着一个数字:732,然后运行这个程序,输出如下:
[Loop 1]: My guess is 500 1) 500 == sn 2) 500 < sn
(本章节未完结,点击下一页翻页继续阅读)