在 turtle.setpos() 中使用算术运算符

Using arithmetic operators in turtle.setpos()

我试图在 setpos() 中将变量 c 除以 2

import turtle
jeff = turtle.Turtle()
jeff.penup()
jeff.shape("square")
jeff.shapesize(.5,.5,.5)
def ask():
    a = raw_input("pick one of the following colors: (red, orange, yellow, green, blue, purple, black)")
    b = raw_input("pick another one of the following colors: (red, orange, yellow, green, blue, purple, black)")
    c = raw_input("how many squares long do you want the patern to be: ")
    jeff.setpos(-(c/2*2+11), c/2*11)
    for lap in range(0, c/2):
        for lap in range(0, c/2):
            jeff.forward(11)
            jeff.color(a)
            jeff.stamp()
            jeff.forward(11)
            jeff.color(b)
            jeff.stamp()
        jeff.right(90)
        jeff.forward(11)
        jeff.right(90)
        for lap in range(0, c/2):
            jeff.color(a)
            jeff.stamp()
            jeff.forward(11)
            jeff.color(b)
            jeff.stamp()
            jeff.forward(11)
        jeff.left(90)
        jeff.forward(11)
        jeff.left(90)

ask()

当我输入数据时它说

TypeError: unsupported operand type(s) for /: 'str' and 'int'
c = raw_input("how many squares long do you want the patern to be: ")

将return用户输入为字符串。将 String 除以 int(在下一行)没有任何意义,会导致您看到的错误。

你想要的是:

c = int(raw_input("how many squares long do you want the patern to be: "))

这将从 raw_input() 获取输入数字作为字符串,然后将其转换为整数。