尝试将 IntergerMath 放在 Python Turtle Graphics 上

Trying to put IntergerMath on Python Turtle Graphics

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

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)

我目前正在努力添加图形以将条形图上的间芽与 python 海龟图形连接起来。然而,当我输入这段代码(输入)时,我遇到了一些问题:

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'

所以我在这里尝试做的是同时使用平均值和距离作为输入,输出应该要求用户插入四个数字,它会在 python 海龟图形上绘制四个条。

那么我怎样才能让这段代码在图形上工作?

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

此处的 y 值被转换为字符串:

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

您可以通过删除 str() 转换来修复它:

y_values = [y1, y2]

doBar()画的是三角形,不是矩形。它需要为矩形的右侧绘制一条长度为 height 的垂直线:

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()