简体   繁体   English

python Traceback(最近一次调用持续):

[英]python Traceback (most recent call last):

I'm trying to create a guessing game, but I'm running into some errors, can anyone help me? 我正在尝试创建一个猜谜游戏,但遇到一些错误,有人可以帮助我吗?

A = A + [i]
# Define two variables which will be the end point of
# the search span 
mini = 0 
maxi = len(A) - 1 
stepcounter = 1 
find = int(input("Think of a number between 1 and 1000")) 
def guessgame(find, A, mini, maxi, stepcounter): 
    # This calculates the value of the midpoint element 
    # in the list guess = (A[mini] + A[maxi]) // 2 
    if guess == find: 
        return 
        print("You think of", guess, "I did this in", stepcounter, "number of steps") 
    elif guess < find: 
        mini = guess 
        stepcounter = stepcounter + 1 
        guessgame(find, A, mini, maxi, stepcounter) 
    elif guess > find: 
        maxi = guess 
        stepcounter = stepcounter + 1 
        guessgame(find, A, mini, maxi, stepcounter) 
    else: 
        return 
        print("Not in array") 
        guessgame(find, A, mini, maxi, stepc

ounter) 猎人)

my error is 我的错误是

traceback most recent call last:
line 34 guessgame(find, a, mini,maxi, supercounter)

File "C:/Users/Acer VN7/PycharmProjects/untitled1/py.py", line 20, in guessgame
if guess == find:
RecursionError: maximum recursion depth exceeded in comparison

The main problem you have is that you confuse the guess with its position. 您遇到的主要问题是将猜测与其位置混淆。 You compute guess to be the midpoint of the values in A , but then you set mini or maxi to the guess , rather than its position. 您将猜测计算为A中值的中点,但随后将mini或maxi设置为guess ,而不是其位置。 I expect that this confusion drives your guess out of the binary search range, and you get an infinite recursion. 我希望这种混淆会使您的猜测超出二进制搜索范围,并且您将获得无限递归。

To debug this problem, add a few print statements to track execution. 要调试此问题,请添加一些打印语句以跟踪执行。 For instance, the first line of your function should be something like 例如,函数的第一行应类似于

print find, mini, maxi

Then, when you compute guess and move one endpoint, print out those values before you leave the routine. 然后,当您计算猜测并移动一个端点时,请在退出例程之前打印出这些值。

You seem to have omitted the code that puts values into A , so I'm not at all sure what's in there. 您似乎已经省略了将值放入A的代码,所以我完全不确定其中有什么。

Beyond that, there are structural problems. 除此之外,还存在结构性问题。 You can't get to the code after return in your if and else branches. 返回 ifelse分支后,您将无法获得代码。 You can't get to the else branch at all, since the first three branches cover all possibilities (law of excluded middle). 您根本无法进入else分支,因为前三个分支涵盖了所有可能性(排除中间法则)。

Does this get you moving? 这会让你动起来吗?

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

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