当前位置: 代码迷 >> 综合 >> 4-5课:为什么每个程序员都要拜四阿哥?要解决bug(八阿哥)
  详细解决方案

4-5课:为什么每个程序员都要拜四阿哥?要解决bug(八阿哥)

热度:8   发布时间:2024-02-21 07:32:36.0

猜数小游戏为什么输了?

改写小助手

有位小A同学,学习了前一章的猜数游戏程序之后,决定自己把攻击者小助手重新实现一遍。

不过小A想:程序写出来是我自己用嘛,何必搞那么麻烦呢?于是去掉了一些限制。另外,最好每一轮都打印出当前是第几轮,这样不是看得更清楚吗?于是,程序变成了下面这样:

low = 1
high = 1000loopNum = 0while low <= high:m = int((high - low) / 2) + lowloopNum += 1print("[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)breakelse:if userInput == '2':
  相关解决方案