Jython 2.5 isdigit

时间:2016-03-11 01:53:42

标签: jes jython-2.5

我正在尝试向程序添加一个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&#39; / program - CPS 201 / PA5Sikorski.py

的第182行

1 个答案:

答案 0 :(得分: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

你可能想要注意那个长表达式的部分顺序(这个或那个或......)。打破它也会使它更具可读性。