Python int/str检查

2024-06-11 19:32:35 发布

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

我有一个家庭作业,我已经完成了作业的最低限度,但在做作业的同时,我对如何使我的程序变得更好很感兴趣。代码的要点是绘制一个用户定义的形状,并在输入无效输入时提供反馈。我想继续这样做,如果一个用户输入一个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()

Tags: andaninputthatisintenterplease
3条回答

我建议使用

not h.isdigit()

检查字符串h是否不包含整数。它不适用于浮点数,因为它真正要检查的是每个数字是否在0-9范围内,并且.不会被(正确地)识别为一个数字。在

例如线路

^{pr2}$

会变成

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

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

在这种情况下,您应该使用异常处理。基本上,其思想是获取输入并尝试将其转换为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')
>>> isinstance('a', int)
False
>>> isinstance(2, int)
True

相关问题 更多 >