2015-06-21 76 views
0

我是Python的新手。 这是我的代码,执行二进制搜索找到猜测的数字。我无法正确地弄清楚如何使我的代码工作。 任何帮助,将不胜感激。谢谢。基本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

这将是对你有帮助 - http://stackoverflow.com/questions/30934834/python-numbers -game-reverse/30935878#30935878 –

+1

这有几个错误。首先,每次在循环开始时将“lo”重置为零,因此后面设置的任何内容都会变回零。将'lo = 0'移到循环之前。无论如何,这是一个开始,尽管如此。另请参阅https://www.python.org/dev/peps/pep-0008/ – cdarke

+0

@VivekSable谢谢。 –

回答

1

以下几点需要申请代码:

  1. 定义下限和上限for循环之外becsue如果我们里面while loop定义,每次lohi变量将与0100值分别创建。
  2. 根据变量工作给变量名称。

lower = 0 higher = 100

  • 神实践Write函数包裹代码。

  • 由于猜测数为更高然后设置最大数量猜数。 由于猜数是较低然后设置最小数字猜数。

  • 演示:

    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() 
    

    输出:

    [email protected]:~/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 
    
    +0

    非常感谢你。 :) –

    +1

    @ M.UsmanSiddiqui:欢迎。免费提问stackoverflow或我[我可以通过电子邮件到达[email protected]或skpe vivek.igp] –

    +0

    我已经放弃了电子邮件检查了兄弟。 –