【问题标题】:Jython 2.5 isdigitJython 2.5 isdigit
【发布时间】:2016-03-14 02:02:19
【问题描述】:

我正在尝试将 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 行

【问题讨论】:

    标签: jes jython-2.5


    【解决方案1】:

    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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多