小编Jay*_*ope的帖子

在Python中减少递归深度的方法

我目前正在制作一个利用填充递归的程序(例如将带有黑色边框的白色圆圈填充为不同的颜色).当我点击我的图像进行填充时,只有部分圆圈会被填充到不同的颜色,然后我得到递归错误.我的代码中唯一有递归的部分就是这个.

def floodfill(x,y):
    floodfill(x+1,y)
    floodfill(x-1,y)
    floodfill(x, y+1)
    floodfill(x, y-1)
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
158
查看次数

使用字典尝试并排除异常 (Python)

假设我向用户询问一个单词,如果该单词不是字典中的键,那么我想打印“该单词不是字典中的键,请重试”。我将如何使用 try 和 except 来做到这一点?这是我到目前为止所拥有的。

dict = {"These": 1, "are": 2, "words": 3}
while True:
    try:
        w = input("Enter a word: ")
    except: 
        print("That word is not a key in the dictionary, try again")
    else:
        print("That word is a key in the dictionary")
Run Code Online (Sandbox Code Playgroud)

python dictionary try-except

-1
推荐指数
1
解决办法
9241
查看次数

标签 统计

python ×2

dictionary ×1

try-except ×1