2017-05-17 28 views
1

我的程序被卡住了,但它告訴你在選定的數字範圍內想一個數字,我不確定如何解決這個問題。程序卡在反向蟒猜謎遊戲

我不完全確定我在做什麼,但程序的開始正常工作。

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

你的'while True:guess = guessNumber(min_no,max_no)'在任何情況下都不會中斷。難怪這是一個無限循環 – kuro

+0

爲什麼在_guessNumber_中的return語句之後有代碼? – CIsForCookies

+0

而真正是造成問題的那個 – Eliethesaiyan

回答

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

這段代碼爲你做它。你的代碼有很多錯誤: -

1.)無限while循環(while True: guess = guessNumber(min_no,max_no))。你不需要這個問題的列表/數組。

3.)while True: guess = guessNumber(min_no,max_no),這個函數是錯誤的。既然你正在返回值,它不會超過except語句。

4.)不需要def userNoInput(msg):函數,反正沒有問題。

5)最後一行(打印說法是錯誤的太)

希望這將幫助你

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)