简体   繁体   English

程序卡在反向python猜谜游戏上

[英]Program stuck on reverse python guessing game

My program is stuck while it tells you to think of a number in the range of selected numbers and I'm unsure how to fix this. 我的程序被卡住了,它告诉你要考虑所选数字范围内的数字,我不确定如何解决这个问题。

I'm not entirely sure what I'm doing even but the start of the program works properly. 我不完全确定我在做什么,但程序的开始工作正常。

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.")

desired output: 期望的输出:

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.

Yep, the infinite loop in over first guessing caused program to hang. 是的,在第一次猜测中的无限循环导致程序挂起。 There should be only one event loop, something like this: 应该只有一个事件循环,如下所示:

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

This code will do it for you . 这段代码将为您完成。 There were many mistakes in that code of yours :- 你的代码中有很多错误: -

1.) Infinite While loop ( while True: guess = guessNumber(min_no, max_no) ) . 1.)Infinite While循环(而True:guess = guessNumber(min_no,max_no))。

2.) You dont need lists/arrays for this problem. 2.)你不需要列表/数组来解决这个问题。

3.) while True: guess = guessNumber(min_no, max_no) , this function is wrong . 3.)while True:guess = guessNumber(min_no,max_no),这个函数是错误的。 since you are returning the value ,it is not going past the except statement. 因为要返回值,所以它不会超过except语句。

4.) No need of def userNoInput(msg): function , anyways no issues. 4.)不需要def userNoInput(msg):函数,反正没有问题。

5.) The last line ( Print statement is wrong too ) 5.)最后一行(Print语句也错了)

Hoping this will help you 希望这会对你有所帮助

PS:- Just a suggestion , whenever you get an infinite loop or some issues like that , you should firstly try yourself by printing statements inside loop and see where the issue is . PS: - 只是一个建议,无论何时你得到一个无限循环或类似的问题,你应该首先尝试通过在循环内打印语句并查看问题所在。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM