我们的任务是编写将一个秘密号码猜测从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
作为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
该错误将其更改为:
print "Game over. Your secret number was:",mid
对于遍地的输出50,在一段时间print secretnum
改变:
print "Is your secret number " + str(mid) + "?"
当你在一开始设定secretnum="Is your secret number " + str(mid) + "?"
,它会创建一个字符串,字符串从中期完全分开。所以,如果你改变中期,变化不会被字符串中看到。
Python中的字符串是不可改变的意义,一旦它们是由他们完成。你不能改变一个字符串的内容,而没有完全改造它。什么str(mid)
确实是创造mid
的字符串表示。在这种情况下,创建的字符串"50"
并投入字符串,永远不会被修改。所以,当你显示一个字符串,你需要确保它被再次调用str(mid)
显示最新的值。
在最后一行,你简单地说:
print "Game over. Your secret number was: " + str(mid)
这是因为蟒蛇要确保你真的知道自己在做什么 - 那就是增加了两个字符串连接在一起,而不是一个int和一个字符串 - 这是被禁止的。该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)
更改行20
print "Game over. Your secret number was: " + mid
至
print "Game over. Your secret number was: ", mid
要么
print "Game over. Your secret number was: " + str(mid)
在最后一行,键入print "Game over. Your secret number was: " + mid
。然而,mid
是一个数字,并将其添加到字符串没有任何意义。您需要先将其转换为字符串。
有几种方法可以做到这一点。你可以将其更改为:
print "Game over. Your secret number was: " + str(mid)
...把mid
成字符串print "Game over. Your secret number was: ", mid
......这样Python将把字符串作为mid
两个变量,并自动将它们之间添加空格.
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)
当我建立我的代码,我在第一次有同样的问题。寻找你的代码后,我已经注意到了一些错误,我会在这里强调它们:
while mid != num:
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 too low. Enter 'c' to indicate I guessed correctly. ")
看到这个在这里,你可以使用一段时间,所以它运行直到中间比用户选择了一些不同的,以具有打印therem这是非常重要的,否则你将无法看到该程序的新猜测你选择,如果后第一猜测是太低或太高。
break
elif herp == 'l':
low = mid
elif herp == 'h':
high = mid
else:
print"Sorry, I did not understand your input."
mid = int(low + high)/2
print "Game over. Your secret number was: " + str(mid)
请注意,我用了“破发”离开while循环等消息游戏结束。你的秘密号码是:“+ STR(MID)打印。最后的结果应该是这样的:
low = 0
high = 100
mid = (high + low)/2
num = raw_input("Please think of a number between 0 and 100!")
while mid != num:
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 too low. Enter 'c' to indicate I guessed correctly. ")
if herp == 'c':
break
elif herp == 'l':
low = mid
#mid = int(mid + high)/2
elif herp == 'h':
high = mid
#mid = int(mid + low)/2
else:
print"Sorry, I did not understand your input."
mid = int(low + high)/2
print "Game over. Your secret number was: " + str(mid)
试试下面的代码...
max_num=100
min_num=0
guess = int(abs(max_num/2))
print("Please think of a number between 0 and 100!")
for i in range(min_num,max_num):
text = "Is your secret number "+str(guess)+"?\nEnter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. "
xx = raw_input(text)
if(xx == 'h'):
max_num = guess
guess = int((guess + min_num)/2)
elif(xx == 'l'):
min_num = guess
guess = int((guess + max_num)/2)
elif(xx == 'c'):
print("Game over. Your secret number was:"+str(guess))
else:
print("Sorry, I did not understand your input.")
xx = str(input(text))
您不必包括int((mid + low)/2)
under每if-elif
块。下面是相同的行使批准的代码:
print("Please think of a number between 0 and 100!")
low = 0
high = 100
guess = (low + high) // 2
while True:
print("Is your secret number " + str(guess) + "?")
suggest = 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 suggest == 'l':
low = guess
elif suggest == 'h':
high = guess
elif suggest == 'c':
print("Game over. Your secret number was: " + str(guess))
break
else:
print("Sorry, I did not understand your input.")
guess = (low + high) // 2