基本Python代码不工作

时间:2015-06-21 08:44:39

标签: 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 个答案:

答案 0 :(得分:1)

以下几点需要应用代码:

  1. 如果我们定义内部for,则每次while looplo变量将使用hi创建时,定义0循环之外的下限和上限。分别为100值。
  2. 根据变量工作给出变量名称。
  3. lower = 0 higher = 100

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

    2. 由于猜测数字更高,请将最大数量设置为猜测数字。 由于猜测数字较低,请将最小数字设置为猜测数字。

    3. 演示:

      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