1

我们的任务是编写一个代码,它会从 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
4

8 回答 8

2

对于错误将其更改为:

print "Game over. Your secret number was:",mid

对于一遍又一遍地输出 50,print secretnum在 while 中更改为:

print "Is your secret number " + str(mid) + "?"

当您secretnum="Is your secret number " + str(mid) + "?"在开头设置时,它会创建一个字符串,一个与 mid 完全分开的字符串。因此,如果您更改 mid,则不会在字符串中看到更改。

Python 字符串是不可变的,这意味着一旦创建,它们就完成了。您无法更改字符串的内容,除非完全重新制作它。什么str(mid)是创建一个字符串表示mid。在这种情况下,字符串"50"被创建并放入字符串中,永远不会被修改。str(mid)因此,当您显示一个字符串时,您需要通过再次调用来确保它显示的是最新值。

于 2013-02-13T01:42:16.577 回答
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
于 2013-02-13T02:01:47.783 回答
1

在最后一行中,您只需输入:

print "Game over. Your secret number was: " + str(mid)

这是因为 python 想要确保你真的知道你在做什么——将两个字符串加在一起,而不是一个 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) 
于 2013-02-13T01:47:58.567 回答
0

更改第 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)

于 2013-02-13T01:42:30.330 回答
0

在最后一行,您输入了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)
于 2013-02-13T01:43:16.223 回答
0

当我构建我的代码时,我一开始遇到了同样的问题。查看您的代码后,我注意到一些错误,我将在此处突出显示它们:

  1. 您没有使用 raw_input 向用户询问号码
  2. 你不应该在你的时候使用'c'参数
  3. 您的 while 应该出现在 raw_input 问题之后

    num = raw_input("请想一个 0 到 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. ")
    

在这里看到这个,你使用 while 所以它运行直到 mid 与用户选择的数字不同,打印它们非常重要,否则在你选择后你将无法看到程序的新猜测第一次猜测太低或太高。

  1. 无需一遍又一遍地重复“mid = int(low + high)/2”,只需在 else 之后使用它,每次 while 循环运行时,它都会改变 mid 的值。
  2. 您需要以字母“c”开始 if/elif/else 代码
  3. 您可以删除“print secretnum”行和之后的行

    如果 herp == 'c':

            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)
    

    请注意,我使用“break”来离开 while 循环,因此消息 Game over。你的密码是:" + 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)
于 2013-02-16T02:47:32.400 回答
0

试试下面的代码...

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))
于 2016-09-16T09:53:28.043 回答
0

您不必包含int((mid + low)/2)在每个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
于 2019-02-12T09:05:09.957 回答