0

我正在尝试将 isdigit() 添加到程序中,以便我可以验证用户输入的内容是否有效。这就是我到目前为止所拥有的。但是当我运行它并输入一个字符时,说“f”。它崩溃并给我将在代码下方发布的错误。有任何想法吗?

def mirrorHorizontal(source):    
    userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.")      #asks user for an input
    while (int(userMirrorPoint) < 0 or int(userMirrorPoint) > (int(getHeight(source) - 1)//2)) or not(userMirrorPoint.isdigit()):
        userMirrorPoint = requestString("Enter a mirror point from 0 to halfway through the pitcure.") 
    height = getHeight(source)
    mirrorPoint = int(userMirrorPoint)
    for x in range(0, getWidth(source)):
        for y in range(0, mirrorPoint):
            topPixel = getPixel(source, x, y)
            bottomPixel = getPixel(source, x, height-y-1)
            color = getColor(topPixel)
            setColor(bottomPixel, color)

错误是: f 不适当的参数值(类型正确)。尝试将参数传递给函数时发生错误。请检查 /Volumes/FLASHDRIVE2/College/Spring 16'/Programs - CPS 201/PA5Sikorski.py 的第 182 行

4

1 回答 1

0

isdigit() 本身在我本地拥有的 2.7.0 jython 版本中表现自己

>>> '1'.isdigit()
True
>>> ''.isdigit()
False
>>> 'A'.isdigit()
False
>>> 'A2'.isdigit()
False
>>> '2'.isdigit()
True
>>> '22321'.isdigit()
True

尝试分解你的大表达式,因为类型转换为整数会为非数字字符串抛出错误。在 Python 版本中都是如此。

>>> int('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'b'
>>> int('2')
2

您可能需要注意那个长表达式(这个或那个或......)的各个部分的顺序。将其分解也会使其更具可读性。

于 2016-03-13T18:02:19.563 回答