2015-06-21 80 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

    我已經放棄了電子郵件檢查了兄弟。 –