【问题标题】:Using arithmetic operators in turtle.setpos()在 turtle.setpos() 中使用算术运算符
【发布时间】:2019-07-05 23:55:06
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 turtle-graphics


    【解决方案1】:
    c = raw_input("how many squares long do you want the patern to be: ")
    

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

    你想要的是:

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多