python - 基本的 Python 代码不起作用

标签 python algorithm data-structures

我是 Python 新手。 这是我的代码,它实现了二进制搜索来查找猜测的数字。我无法正确地弄清楚如何使我的代码工作。 任何帮助,将不胜感激。谢谢。

print"Please think of a number between 0 and 100!"



guessed=False
while not guessed:
    lo=0
    hi=100
    mid=(lo+hi)/2
    print'Is you Secret Number'+str(mid)+'?'
    print"Enter 'h' to indicate the guess is too high.",
    print"Enter'l' to indicate the guess is too low",
    print"Enter'c'to indicate I guessed correctly"
    x=raw_input()
    if(x=='c'):
        guessed=True
    elif(x=='h'):
        #Too High Guess
        lo=mid+1
    elif(x=='l'):
        lo=mid-1
    else:
        print("Sorry, I did not understand your input.")
print'Game Over','Your Secret Number was'+str()

最佳答案

以下几点需要申请代码:

  1. for 循环之外定义下限和上限,因为如果我们在 while 循环 内定义,每次 lohi 变量将分别创建 0100 值。
  2. 根据变量的工作给出变量名。

较低 = 0 更高 = 100

  1. 上帝练习编写函数来包装您的代码。

  2. 因为猜测的数字更高 然后设置最大数字 来猜测数字。 由于猜测数较低 然后将最小数 设置为猜测数。

演示:

import time


def userNoInput(msg):
    """ Get Number into from the user. """
    while 1:
        try:
            return int(raw_input(msg))
        except ValueError:
            print "Enter Only Number string."
            continue


def guessGame():
    """
        1. Get Lower and Upeer Value number from the User.
        2. time sleep to guess number for user in between range.
        3. While infinite loop.
        4. Get guess number from the Computer.
        5. User can check guess number and tell computer that guess number if correct ror not.
        6. If Correct then print msg and break While loop.
        7. If not Correct then 
            Ask Computer will User that guess number is Greate or Lower then Actual number. 
            7.1. If Greater then Set Max limit as guess number.  
            7.2. If Not Greater then Set Min limit as guess number.
            7.3. Continue While loop
    """
    min_no = userNoInput("Please input the low number range:")
    max_no = userNoInput("Please input the high number range:")
    print "Guess any number between %d and %d."%(min_no, max_no)
    time.sleep(2)

    while True:
        mid = (min_no+max_no)/2
        print'Is you Secret Number'+str(mid)+'?'
        print"Enter 'h' to indicate the guess is too high.",
        print"Enter'l' to indicate the guess is too low",
        print"Enter'c'to indicate I guessed correctly"
        x=raw_input().lower()
        if(x=='c'):
            print'Game Over','Your Secret Number was'+str(mid)
            break
        elif(x=='h'):
            #- As guess number is higher then set max number to guess number. 
            max_no=mid - 1
        elif(x=='l'):
            #- As guess number is lower then set min number to guess number. 
            min_no = mid + 1
        else:
            print("Sorry, I did not understand your input.")

guessGame()

输出:

vivek@vivek:~/Desktop/stackoverflow$ python guess_game.py 
Please input the low number range:1
Please input the high number range:100          
Guess any number between 1 and 100.
Is you Secret Number50?
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 you Secret Number25?
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 you Secret Number12?
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 you Secret Number18?
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 you Secret Number15?
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 you Secret Number16?
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 you Secret Number17?
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 was17

关于python - 基本的 Python 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962747/

相关文章:

python - 使用 Python 中另一列的值基于多个条件在 DataFrame 中创建新列

algorithm - 数字总和可被 K 整除的子数组数

Java 创建二维链表(数据结构)

algorithm - 该算法的大 O 成本函数是什么?

algorithm - 构建一个数组大小限制为 5 的队列 - 但队列可以增长

c++ - 获取结构中元素的数量

c++ - 检查一个节点是否在有向图的节点的路径中

python - 使用 geopandas 计算多边形的正确面积

python - 将时间列添加到基于另一个 DataFrame 的 DataFrame

python - 非阻塞 multiprocessing.connection.Listener?