Python int/str 检查

1 投票
4 回答
2137 浏览
提问于 2025-04-16 14:18

我有一个作业要做,我只完成了最低要求的部分,但在做作业的过程中,我对如何让我的程序变得更好产生了兴趣。这个代码的目的是绘制用户定义的形状,并在用户输入无效内容时提供反馈。我想让程序在用户输入高度、宽度或坐标时,如果输入的是字符串,就能给出错误提示,但我不太确定该怎么做。因为这是入门课程,所以请记住,我们刚刚学习了while循环,也学习过for循环、if/else等。

这是我目前的代码:


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 个回答

2

在编程中,有时候我们需要处理一些数据,比如从一个地方获取数据,然后把它放到另一个地方。这个过程就像是把水从一个桶倒到另一个桶一样。

有些时候,我们可能会遇到一些问题,比如数据格式不对,或者数据不完整。这就像是你在倒水的时候,发现桶有个洞,水流不进去一样。

为了避免这些问题,我们可以使用一些工具或者方法来检查数据,确保它们是正确的。这样就能顺利地把水(数据)从一个桶倒到另一个桶了。

总之,处理数据的时候要小心,确保每一步都正确,这样才能得到想要的结果。

>>> isinstance('a', int)
False
>>> isinstance(2, int)
True
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,因为如果你用的是 Python 2.x,你的代码就无法正常工作,因为在 Python 2.x 中,input 的工作方式是不同的。在 Python 3.x 中,它总是返回一个字符串,所以没有必要检查返回的对象是否是字符串。

6

在这种情况下,你需要使用异常处理。简单来说,就是先尝试把输入的内容转换成一个整数(int)。如果转换失败,就会出现一个叫做ValueError的错误。要是转换成功,你就能得到转换后的值。记住,输入的内容总是以字符串(str)的形式给你,所以你不能直接检查它是不是整数。如果你使用的是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')

撰写回答