代码之家  ›  专栏  ›  技术社区  ›  Anton O. Obyedkov

试图在Python Turtle图形上添加IntergerMath

  •  0
  • Anton O. Obyedkov  · 技术社区  · 7 年前

    因为我已经完成了平均值和距离的编码:

    x1=eval(input("Please insert a first number: "))
    y1=eval(input("Please insert a second number: "))
    x2=eval(input("Please insert a third number: "))
    y2=eval(input("Please insert a fourth number: "))
    add = x1
    add = add + y1
    add = add + x2
    add = add + y2
    average = add/4
    d= distanceFormula(x1,y1,x2,y2)
    print("Average:", average)
    print("Distance:", d)
    

    我现在正在添加图形,将条形图上的intergermath与python turtle图形连接起来。然而,我在键入此代码(输入)时遇到了一些问题:

    def doBar(height, clr):
       begin_fill()
       color(clr)
       setheading(90)
       forward(height)
       right(90)
       forward(40)
       right(90)
       end_fill()
    
    y_values = [str(y1), str(y2)]
    x_values = [str(x1), str(x2)]
    colors= ["red", "green", "blue", "yellow"]
    up()
    goto(-300, -200)
    down()
    idx = 0
    for value in y_values:
        doBar(value, colors[idx])
        idx += 1
    

    以下是输出结果,在正常运行后,我得到了一些错误:

    Traceback (most recent call last):
     in main
     doBar(value, colors[idx])
     in doBar
     forward(height)
     line 1637, in forward
     self._go(distance)
     line 1604, in _go
     ende = self._position + self._orient * distance
     line 257, in __mul__
     return Vec2D(self[0]*other, self[1]*other)
    TypeError: can't multiply sequence by non-int of type 'float'
    

    那么,我如何使这段代码在图形上工作呢?

    1 回复  |  直到 7 年前
        1
  •  0
  •   mhawke    7 年前

    height 正在传递到 doBar() 一串 . 然后,函数将字符串传递给 forward() 但是,该函数需要整数或浮点。

    y_values = [str(y1), str(y2)]
    

    您可以通过删除 str() 转换:

    y_values = [y1, y2]
    

    多巴() 身高

    def doBar(height, clr):
        begin_fill()
        color(clr)
        setheading(90)
        forward(height)
        right(90)
        forward(40)
        right(90)
        forward(height)    # draw back down to the x-axis
        end_fill()