上Python课程,学习二分查找,疑惑为何我的语法不正确
我们的任务是写一段代码,来猜一个从0到100之间的秘密数字。
这是我写的代码:
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
8 个回答
1
在最后一行,你只需要写:
print "游戏结束。你的秘密数字是: " + str(mid)
这是因为Python想确保你真的知道自己在做什么——也就是说,它要你把两个字符串加在一起,而不是把一个整数和一个字符串加在一起,这样是禁止的。str()这个函数的作用就是把你给它的任何东西都转换成字符串。关于你提到的语义问题,我觉得这个版本的代码表现得很正常:
low = 0
mid = 50
high = 100
print "Please think of a number between 0 and 100!"
herp = 50
while herp != 'c':
print "Is your secret number " + str(mid) + "?"
herp = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is to low. Enter 'c' to indicate the guess is correct")
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."
if herp == 'c':
print "Game over. Your secret number was: " + str(mid)
2
对于这个错误,把它改成:
print "Game over. Your secret number was:",mid
关于一直输出50的问题,把while循环里的 print secretnum
改成:
print "Is your secret number " + str(mid) + "?"
当你在一开始设置 secretnum="Is your secret number " + str(mid) + "?"
时,它会生成一个字符串,这个字符串和mid是完全分开的。所以如果你改变了mid,字符串里的内容不会跟着改变。
在Python中,字符串是不可变的,这意味着一旦创建了字符串,它就不能被修改。你不能直接改变字符串的内容,必须重新创建一个新的字符串。str(mid)
的作用是把 mid
转换成字符串。在这个例子中,会生成字符串 "50"
并放入到字符串中,这个字符串是不会被修改的。因此,当你要显示一个字符串时,需要确保它显示的是最新的值,这就需要再次调用 str(mid)
。
2
正如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