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

时间:2019-07-05 23:55:06

标签: python python-2.7

我试图在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'

1 个答案:

答案 0 :(得分:3)

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

将以字符串形式返回用户输入。将String除以int(在下一行中)没有任何意义,并且会导致您看到错误。

您想要的是:

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

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