程序停留在反向python猜谜游戏

时间:2017-05-17 05:12:28

标签: python

我的程序被卡住了,因为它告诉您考虑所选数字范围内的数字,我不确定如何解决这个问题。

我不完全确定我做了什么,但程序的开始工作正常。

from random import randrange
import time


def guessNumber(min_no, max_no):
   try:
      return randrange(min_no, max_no)
   except ValueError:
       return min_no
   left = 0
   right = len(L)-1
   while left<=right:   
      m = (left+right)//2
      if x == L[m]:
         return m
      elif x < L[m]:
         right = m-1
      else:  #  L[m] < x 
         left = m+1


def userNoInput(msg):
   while 1:
      try:
         return int(input(msg))
      except:
         print("Numbers Only !")
         sys.exit(0)

print("Enter two numbers, low then high.")
min_no = userNoInput("low = ")
max_no = userNoInput("high = ")
print ("Think of a number in the range %d and %d."%(min_no, max_no))
max_no += 1

while True:
   guess = guessNumber(min_no, max_no)
count = 0
L = []
for i in range(low, high+1):
   L.append(i)

while True:
   print("Is your number Less than, Greater than, or Equal to %d ?") % (L[(int(len(L)/2))])
   guess = guessNumber(low, high)
   guess = input("Type 'L', 'G' or 'E': ")
   guess = guess.upper()
   if guess == 'E':
      break
   elif guess == 'L': guess < number
   elif guess == 'G': guess > number

   else:
      print (guess)

print ("Your number is" + guess + "." + "I Found it in" + number + "guesses.")
  
    

期望的输出:

  
Enter two numbers, low then high.
low = 2
high = 18
Think of a number in the range 2 to 18.
Is your number Less than, Greater than, or Equal to 10?
Type 'L', 'G' or 'E': g
Is your number Less than, Greater than, or Equal to 14?
Type 'L', 'G' or 'E': L
Is your number Less than, Greater than, or Equal to 12?
Type 'L', 'G' or 'E': L
Your number is 11. I found it in 3 guesses.

2 个答案:

答案 0 :(得分:0)

是的,第一次猜测的无限循环导致程序挂起。 应该只有一个事件循环,如下所示:

from random import randrange
import time


def guess_number(min_no, max_no):
   return randrange(min_no, max_no)


def user_num_input(msg):
   try:
      return int(raw_input(msg))
   except ValueError:
      print('Numbers Only!')
      sys.exit(1)


def get_hint(current_guess):
   return raw_input('Is your number Less than, Greater than, or Equal to %d? (L, G, E): ' % current_guess)


print('Enter two numbers, low then high.')
min_no = user_num_input('low = ')
max_no = user_num_input('high = ')
assert max_no >= min_no
print ('Think of a number in the range %d and %d.' % (min_no, max_no))

iteration = 1
while True:
   guess = guess_number(min_no, max_no)
   hint = get_hint(guess)
   while hint not in ['E', 'L', 'G']:
      print('You gave unknown answer. Please retry.')
      hint = get_hint(guess)

   if hint == 'E':
      break
   elif hint == 'L': 
      max_no = guess
   elif hint == 'G': 
      min_no = guess
   else:
      raise ValueError('Unreachable!')

   iteration += 1

print ('Your number is %d. I found it in %d guesses.' % (guess, iteration))

答案 1 :(得分:0)

此代码将为您完成。你的代码中有很多错误: -

1。)Infinite While循环(同时为True:    guess = guessNumber(min_no,max_no))。

2。)你不需要列表/数组来解决这个问题。

3。)而True:    guess = guessNumber(min_no,max_no),这个函数错了。因为你要返回值,所以它不会超过except语句。

4.。)无需def userNoInput(msg):函数,无论如何都没有问题。

5.。)最后一行(Print语句错误)

希望这会对你有所帮助

P.S: - 只是一个建议,无论何时你得到一个无限循环或类似的问题,你应该首先尝试通过在循环内打印语句并查看问题所在。

from random import randrange
import time


def guessNumber(min_no, max_no):
   left = min_no
   right = max_no
   if left == right:
      return left
   m = (left+right)/2
   return m

def userNoInput(msg):
   while 1:
      try:
          return int(input(msg))
      except:
          print("Numbers Only !")
          sys.exit(0)

print("Enter two numbers, low then high.")
min_no = userNoInput("low = ")
max_no = userNoInput("high = ")
# print min_no,max_no
print ("Think of a number in the range %d and %d."%(min_no, max_no))
max_no += 1

count = 0

while True:
   count+=1
   guess = guessNumber(min_no, max_no)
   print("Is your number Less than, Greater than, or Equal to %d ?") %guess
   temp = raw_input("Type 'L', 'G' or 'E': ")
   temp = temp.upper()
   if temp == 'E':   
       break
   elif temp == 'L':
       max_no = guess-1;

   elif temp == 'G':
       min_no = guess+1;

print "Your number is %r . I Found it in %r guesses." %(guess,count)