首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >二分搜索游戏

二分搜索游戏
EN

Code Review用户
提问于 2013-02-21 22:01:04
回答 4查看 9.4K关注 0票数 6

我开始学习Python,并试图优化这个二分法搜索游戏。

代码语言:javascript
复制
high = 100
low = 0
guess = (high + low)/2  


print('Please think of a number between 0 and 100!')

guessing = True
while guessing:
    print('Is your secret number ' + str(guess) + '?')
    pointer = 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 pointer == 'h':
        high = guess
        guess = (low + guess)/2

    elif pointer == 'l':
        low = guess
        guess = (high + guess)/2

    elif pointer == 'c':
        guessing = False

    else:
        print('Sorry, I did not understand your input.')
print('Game over. Your secret number was: ' + str(guess))
EN

回答 4

Code Review用户

回答已采纳

发布于 2013-02-22 00:44:11

我认为有些东西会改进您的代码,这是非常正确的:

  • 有了highlow的变量,就不应该在开头的print中硬编码它们的值。
  • 您应该使用//来确保得到整数除法。
  • 如果将guess = (low + high) // 2作为while循环中的第一行,则只能编写一次。
  • 在检查pointer时,您可能希望首先将其转换为小写,以确保hH都被理解。
  • 使您的代码在最大行长等方面符合PEP8
  • 使用format方法的str可以更清楚地显示您正在打印的内容。

把这一切结合在一起:

代码语言:javascript
复制
high, low = 100, 0

print('Please think of a number between {0} and {1}!'.format(low, high))

guessing = True
while guessing:
    guess = (low + high) // 2
    print('Is your secret number {0}?'.format(guess))
    pointer = 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.").lower()
    if pointer == 'h' :
        high = guess
    elif pointer == 'l' :
        low = guess
    elif pointer == 'c':
        guessing = False
    else:
        print('Sorry, I did not understand your input.')

print('Game over. Your secret number was {0}.'.format(guess))
票数 8
EN

Code Review用户

发布于 2013-02-22 16:15:21

除了Jaime的观点。

  1. 去掉guessing标志,只需有一个带有break语句的无限循环。
  2. pointer是该变量的一个非常通用的名称,特别是因为它在其他编程语言中意味着其他一些东西。
票数 6
EN

Code Review用户

发布于 2015-09-07 10:31:15

在完成杰米的答复后,请注意:

键入'c'时,即使数字不是您所想的,始终打印代码print('Game over. Your secret number was {0}.'的这一部分。

因此,为了避免这种情况,还必须在(str(numbers) == str(guess))的分支上测试(response == 'c')

代码语言:javascript
复制
high, low = 100, 0
guess = (low + high) // 2

numbers = raw_input('Please think of a number between {0} and {1}!'.format(low, high))

guessing = True
while guessing:

    print('Is your secret number {0}?'.format(guess))
    response = 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.").lower()
    if response == 'h' :
        high = guess
    elif response == 'l' :
        low = guess
    elif (response == 'c') and (str(numbers) ==  str(guess)) :       
            print('Game over. Your secret number was {0}.'.format(guess))
            break
    else:
        print('Sorry, I did not understand your input.')
    guess = (low + high) // 2
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/22984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档