- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我们的任务是编写一段代码来猜测 0 到 100 之间的一个 secret 数字。这是我的一段代码:
low = 0
mid = 50
high = 100
secretnum = "Is your secret number " + str(mid) + "?"
print"Please think of a number between 0 and 100!"
print secretnum
herp = 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. ")
while herp != 'c':
if herp == 'h':
high = mid
mid = int((mid + low)/2)
elif herp == 'l':
low = mid
mid = int((mid + high)/2)
else:
print"Sorry, I did not understand your input."
print secretnum
herp = 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 herp == 'c':
print "Game over. Your secret number was: " + str(mid)
这是输出:
Is your secret number 50?
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. h
Is your secret number 50?
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. l
Is your secret number 50?
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. c
Game over. Your secret number was: 37
最佳答案
正如 Raufio 指出的那样,Python 字符串是不可变的。要解决一遍又一遍地重复 50 的问题,您需要在打印出问题时再次调用 str(mid)。例如:
low = 0
mid = 50
high = 100
secretnum = "Is your secret number: "
print"Please think of a number between 0 and 100!"
print secretnum + str(mid) + "?"
herp = 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. ")
while herp != 'c':
if herp == 'h':
high = mid
mid = int((mid + low)/2)
elif herp == 'l':
low = mid
mid = int((mid + high)/2)
else:
print"Sorry, I did not understand your input."
print secretnum + str(mid) + "?"
herp = 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 herp == 'c':
print "Game over. Your secret number was:", mid
关于python - 参加 Python 类(class)并学习二分搜索,对为什么我的语义不正确感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845083/
很荣幸本周六被邀请到百度“英雄汇”移动开发者沙龙,在此次会议中,看到不少的大牛分享不错的干货,今天把我在会议中所听到的内容给大家分享一下。 数据价值 看起来就是广告的会议,其实也给我们统计出很多
我试图部分消耗Kotlin中的序列,以将其拆分。 fun main() { val seq = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).asSequence(
我正在处理一个查询,试图找出您当前的连续出勤记录。所以换句话说,人 1 参加了最后 5 场事件,人 2 参加了最后 6 场事件,等等。我为此获得的数据文件非常简单: Event Number - Un
我找不到只参与 rdd 的方法. take看起来很有希望,但它返回 list而不是 rdd .我当然可以将其转换为 rdd ,但这似乎既浪费又丑陋。 my_rdd = sc.textFile("my
我目前正在从事学术目的的自然语言处理工作,我想获取 Microsoft Research Sentence Completion Challenge 数据集。 不幸的是,它似乎不再可用 on Micr
我正在尝试让所有参加特定 Activity 的 Facebook 好友。 select uid, rsvp_status from event_member where uid IN (SELECT
我们的任务是编写一段代码来猜测 0 到 100 之间的一个 secret 数字。这是我的一段代码: low = 0 mid = 50 high = 100 secretnum = "Is your s
下学期我将使用我的 MacBook Pro 参加基于 Python 的计算机科学类(class)。它将以定制设计为中心 package对于这个类。问题是这个软件包是由 Microsoft Resear
我是一名优秀的程序员,十分优秀!