Python函数卡住了(没有错误),但我不明白为什么

2024-06-23 19:09:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这个函数做了我想要它做的,但是当它完成后,它只是呆在那里,而不是从我调用它的地方继续,我不知道为什么。代码是:

x = 9
y = 9
n = 10
ty = 1
tx = 1

while ty <= y:
    while tx <= x:
        vars()["p" + str(ty) + str(tx)] = 0
        tx += 1
    ty += 1
    tx = 1

tn = 1
while tn <= n:
    vars()["m" + str(tn)] = tn
    tn += 1

t = x * y
tm = n
def recursion(z):
    global tm
    if z < n:
        for x in range(n):
            recursion(z + 1)
    else:
        if tm > 0:
            tv = "m" + str(tm)
            otm = eval(tv)
            while eval(tv) < t - n + tm:
                vars()[tv] = eval(tv) + 1
                print(tv + " = " + str(eval(tv)))
            vars()[tv] = otm + 1
            print(tv + " = " + str(eval(tv)))
            if tm > 1:
                vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1
                print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1)))))
            tm -= 1

recursion(1)

print("done")

我已经把回报放在我期望它结束的地方,但据我所知,它实际上并不需要它。在

有人能看到我做了什么让它卡住吗?在

谢谢


Tags: 函数if地方evalvarstvtntm
3条回答

我无法计算出发生了什么(结果是如果我离开它几分钟,它实际上会结束),相反,我意识到我不需要使用递归来实现我想要的(我也意识到函数实际上并没有做我想做的事情)。在

对于感兴趣的人,我简化并重写为几个while循环:

x = 9
y = 9
t = x * y
n = 10

tt = 1
while tt <= t:
        vars()["p" + str(tt)] = 0
        tt += 1

tn = 1
while tn <= n:
    vars()["m" + str(tn)] = tn
    vars()["p" + str(tn)] = 1
    tn += 1

def cl():
    w = ""
    tt = 1
    while tt <= t:
        w = w + str(eval("p" + str(tt)))
        tt += 1
    p.append(w)

tm = n
tv = "m" + str(tm)
p = []
while m1 < t - n + tm - 1:    
    cl()
    while tm == n and eval(tv) < t - n + tm:
        vars()["p" + str(eval(tv))] = 0
        vars()[tv] = eval(tv) + 1
        vars()["p" + str(eval(tv))] = 1
        cl()        
    tm -= 1
    tv = "m" + str(tm)
    while tm < n and tm > 0:
        if eval(tv) < t - n + tm:
            vars()["p" + str(eval(tv))] = 0
            vars()[tv] = eval(tv) + 1
            vars()["p" + str(eval(tv))] = 1
            while tm < n:
                tm += 1
                ptv = tv
                tv = "m" + str(tm)
                vars()["p" + str(eval(tv))] = 0
                vars()[tv] = eval(ptv) + 1
                vars()["p" + str(eval(tv))] = 1
        else:
            tm -= 1
            tv = "m" + str(tm)

对于没有经历过变化历史的人,请注意:这是基于对其他答案的评论。更新:更好的版本。在

import itertools

def permutations(on, total):
    all_indices = range(total)
    for indices in itertools.combinations(all_indices, on):
        board = ['0'] * total
        for index in indices:
            board[index] = '1'
        yield ''.join(board)

如果有人对原始代码做了什么感兴趣,我会重新排列条件语句来修剪函数调用树:

x = 9
y = 9
n = 10
ty = 1
tx = 1

while ty <= y:
    while tx <= x:
        vars()["p" + str(ty) + str(tx)] = 0
        tx += 1
    ty += 1
    tx = 1

tn = 1
while tn <= n:
    vars()["m" + str(tn)] = tn
    tn += 1

t = x * y
tm = n
def recursion(z):
    global tm
    if tm > 0:
        if z < n:
            for x in range(n):
                recursion(z + 1)
        else:
            tv = "m" + str(tm)
            otm = eval(tv)
            while eval(tv) < t - n + tm:
                vars()[tv] = eval(tv) + 1
                print(tv + " = " + str(eval(tv)))
            vars()[tv] = otm + 1
            print(tv + " = " + str(eval(tv)))
            if tm > 1:
                vars()["m" + str(tm - 1)] = eval("m" + str(tm - 1)) + 1
                print(str("m" + str(tm - 1) + " = " + str(eval("m" + str(tm -1)))))
            tm -= 1

recursion(1)

print("done")

通过使用列表和范围对象可以使这一点更加清楚,但这需要努力。在

相关问题 更多 >

    热门问题