错误(无限循环或其他)导致我的代码中途停止在shell中运行

2024-10-04 05:26:21 发布

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

虽然我不能确切地说出原因是什么,但代码中的某些内容导致它中途停止运行,使shell处于一种可以单击行、键入、按enter键等的状态。但是shell本身没有给我任何反应。你知道吗

完整代码如下:

from PIL import Image
import PIL.ImageOps


background = "background.png"
target = input("Please enter the name of your image (including file type)")
targetImage = Image.open(target)
background = Image.open(background)
area = targetImage.size 
backArea = background.size
area = (round(area[0] / 500), round(area[1] / 500))
backArea = (round(backArea[0] / 100), round(backArea[1] / 100))
targetImage = targetImage.resize(area)
background = background.resize(backArea)
size = area[0] * area[1]
targetWidth = area[0]
targetLength = area[1]

targetCoords = []


def analyser():
    pixelOne = 1
    pixelTwo = 1
    completedSearch = 0


    while completedSearch != size:
        while pixelOne != targetWidth:
            while pixelTwo != targetLength:
                targetCoords.append((pixelOne, pixelTwo))
                pixelTwo = pixelTwo + 1
                completedSearch = completedSearch + 1
            pixelTwo = 1
            pixelOne = pixelOne + 1


    colouredCoordsSum = []
    largerColouredCoordsSum = []
    smallerColouredCoordsSum = []
    flatTargetCoords = [x for sets in targetCoords for x in sets]
    coordLength = len(flatTargetCoords) - 1
    for i in range(0, coordLength, 2):
        firstnum = flatTargetCoords[i]
        secondnum = flatTargetCoords[i+1]
        sumnum = firstnum + secondnum
        if firstNum > secondNum:
            largerColouredCoordsSum.append("(",firstNum,", ",secondNum,"), (",sumnum,")")
        else:
            smallerColouredCoordsSum.append("(",firstNum,", ",secondNum,")")
        colouredCoordsSum.append(sumnum)

    global topLeft
    global bottomRight
    global topRight
    global bottomLeft
    topLeft = min(smallerColouredCoordsSum)
    bottomRight = max(largerColouredCoordsSum)
    topRight = topLeft + (area[0] - 1)
    bottomLeft = bottomRight - (area[1] - 1)


analyser()
print(topLeft, topRight, bottomLeft, bottomRight)

我认为代码的问题在于第一个while循环的内容:

while completedSearch != size:
        while pixelOne != targetWidth:
            while pixelTwo != targetLength:
                targetCoords.append((pixelOne, pixelTwo))
                pixelTwo = pixelTwo + 1
                completedSearch = completedSearch + 1
            pixelTwo = 1
            pixelOne = pixelOne + 1

也就是说,可能还有其他错误导致了这种情况。我无法在删除错误方面取得进展,因为在这种状态下,代码不能在shell中完全运行。感谢您的帮助。你知道吗

编辑:当我运行代码时,会得到输入提示,但是在键入文件名并按enter键之后,我看不到任何运行的内容,也看不到下一行的“>;>>”,但是我可以在空行之间单击,键入,然后在这些行上按enter键,但是shell不会对我在该状态下键入的任何内容做出反应。完成后,应将每个角点的坐标打印到壳上,但不会出现这种情况。你知道吗


Tags: 代码内容size键入areashellbackgroundenter
1条回答
网友
1楼 · 发布于 2024-10-04 05:26:21
while completedSearch != size:
    while pixelOne != targetWidth:
        while pixelTwo != targetLength:
            targetCoords.append((pixelOne, pixelTwo))
            pixelTwo = pixelTwo + 1
            completedSearch = completedSearch + 1
        pixelTwo = 1
        pixelOne = pixelOne + 1

当这个循环开始时,如果completedSearch大于size,或者pixelOne大于targetWidth,或者pixelTwo大于targetLength,它将永远不会退出。你知道吗

相关问题 更多 >