Python int / str检查

时间:2011-03-24 06:02:47

标签: python loops int string

我有一份家庭作业,我完成了最低限度的任务,但在完成任务时,我开始对如何使我的课程变得更好感兴趣。代码的要点是绘制用户定义的形状,并在输入无效输入时提供反馈。我想继续这样做,如果用户输入高度/宽度的str或其中一个坐标给出错误,但我无法确切知道如何做到这一点。这是一个介绍类,所以如果你记住我们只是进入while循环,我们已经过了循环,if / else / etc。

以下是我目前的代码:


def draw_user_shape(pen):
    shape = input("Please enter a shape: ")
    while shape != "square" and shape != "rectangle" and shape != "triangle":
        shape = input("Invalid shape, please enter either square, rectangle, or triangle: ")
    if shape == "square":
        h = input("Please enter a height: ")
        while h != int or int(h) < 1:
            h = input("That is an invalid height, please enter an positive integer: ")
        h = int(h)
        c = input("Please enter a color: ")
        while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
            c = input("That is an invalid color, please enter either red, blue, or green: ")
        c = str(c)
        x = input("Please enter an x-coordinate: ")
        while x != int and int(x) == 1:
            x = input("That is an invalid x-coordinate, please enter an integer: ")
        x = int(x)
        y = input("Please enter a y-coordinate: ")
        while y != int and int(y) == int:
            y = input("That is an invalid y-coordinate, please enter an integer: ")
        y = int(y)
        pen.fillcolor(c)
        pen.up()
        pen.goto(x,y)
        pen.down()
        pen.begin_fill()
        pen.goto(x,y+h)
        pen.goto(x+h,y+h)
        pen.goto(x+h,y)
        pen.goto(x,y)
        pen.end_fill()
        pen.up()
    elif shape == "rectangle":
        h = input("Please enter a height: ")
        while h != int and int(h) < 1:
            h = input("That is an invalid height, please enter an positive integer: ")
        h = int(h)
        w = input("Please enter a width: ")
        while w != int and int(w) < 1:
            w = input("That is an invalid height, please enter an positive integer: ")
        w = int(w)
        c = input("Please enter a color: ")
        while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
            c = input("That is an invalid color, please enter either red, blue, or green: ")
        c = str(c)
        x = input("Please enter an x-coordinate: ")
        while x != int and int(x) == 1:
            x = input("That is an invalid x-coordinate, please enter an integer: ")
        x = int(x)
        y = input("Please enter a y-coordinate: ")
        while y != int and int(y) == int:
            y = input("That is an invalid y-coordinate, please enter an integer: ")
        y = int(y)
        pen.fillcolor(c)
        pen.up()
        pen.goto(x, y) 
        pen.down()
        pen.begin_fill()
        pen.goto(x,y+h)
        pen.goto(x+w,y+h)
        pen.goto(x+w,y)
        pen.goto(x,y)
        pen.end_fill()
        pen.up()
    elif shape == "triangle":
        h = input("Please enter a height: ")
        while h != int and int(h) < 1:
            h = input("That is an invalid height, please enter an positive integer: ")
        h = int(h)
        w = input("Please enter a width: ")
        while w != int and int(w) < 1:
            w = input("That is an invalid height, please enter an positive integer: ")
        w = int(w)
        c = input("Please enter a color: ")
        while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
            c = input("That is an invalid color, please enter either red, blue, or green: ")
        c = str(c)
        x = input("Please enter an x-coordinate: ")
        while x != int and int(x) == 1:
            x = input("That is an invalid x-coordinate, please enter an integer: ")
        x = int(x)
        y = input("Please enter a y-coordinate: ")
        while y != int and int(y) == int:
            y = input("That is an invalid y-coordinate, please enter an integer: ")
        y = int(y)
        pen.fillcolor(c)
        pen.up()
        pen.goto(x,y)
        pen.down()
        pen.begin_fill()
        pen.goto(x+w/2,y+h)
        pen.goto(x+w,y)
        pen.goto(x,y)
        pen.end_fill()
        pen.up()

def main():
    import turtle
    pen = turtle.Turtle()
    draw_user_shape(pen)

main()

4 个答案:

答案 0 :(得分:6)

在这种情况下,您希望使用异常处理。基本上我的想法是接受输入并尝试转换为int。如果失败,则会引发ValueError。否则,您将获得转换后的值。请记住,输入将始终以str的形式提供给您,因此您无法简单地测试它是否为int。假设您使用的是Python 3,您可以执行类似的操作,直到输入正确的值:

# keep asking for input until we get the right one
while True:
    myInput = input('give me a number: ')
    try:
        myValue = int(myInput)
        # if we reach this point, that means we got our number
        break # this will jump out of the loop
    except ValueError:
        # if we reach this point, that means the input was bad
        print('invalid input')

答案 1 :(得分:3)

我建议使用

not h.isdigit()

检查字符串h是否不包含整数。它不适用于浮点数,因为它实际检查的是每个数字是否在0-9范围内,并且.将无法(正确)识别为数字。

例如

while h != int or int(h) < 1:

会变成

while not h.isdigit() or int(h) < 1:

顺便说一句,我假设您使用的是Python 3.x,否则您的代码将无法正常工作,因为input在Python 2.x中的工作方式不同。在Python 3.x中,它应该总是返回一个字符串,因此没有任何理由检查返回的对象是否为字符串。

答案 2 :(得分:2)

>>> isinstance('a', int)
False
>>> isinstance(2, int)
True

答案 3 :(得分:0)

使用.isdigit().isalnum() 进行检查 ,具体取决于您的喜好

  1. 示例

    str_num= 'aaaa7777'
    str_num.isalnum() # true
    str_num.isdigit() # false
    
  2. 示例

    str_num= 'aaaa 7777' # Note that has spaces
    str_num.isalnum() # false
    str_num.isdigit() # false
    
  3. 示例

    str_num= '7777' 
    str_num.isalnum() # True
    str_num.isdigit() # True