0

Since I'm done with making this code of the Average and the Distance:

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)

I am now currently working on adding graphics to connect intergermath on a bar graph with python turtle graphics. However, I came across upon with some problems when I'm typing this code (Input):

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

And here's the result on the output that I got some errors after it went out as normal:

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'

So am I trying to do here is using both average and distance as an input and the output should be asking a user to insert four numbers and it will draw four bars on a python turtle graphics.

So how can I make this code work on graphics?

1 Answers1

0

height is being passed into doBar() as a string. The function then passes the string to the forward() function, however, that requires either an integer or a float.

The y values are converted to strings here:

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

You can fix it by removing the str() conversion:

y_values = [y1, y2]

doBar() draws a triangle, not a rectangle. It needs one more vertical line of length height to be drawn for the right hand side of the rectangle:

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()
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • All right new problem: Despite that you did helped me solving that problem of yours, I got a result that it only drew two triangles but not bars as I originally thought. – Anton O. Obyedkov Oct 03 '17 at 02:59
  • @AntonO.Obyedkov: look at your `doBar()` function. It draws a line up `height`, then draws a line of length 40 to the right, then stops and fills. There are only 2 lines so that makes a triangle. You need to add another call to `forward(height)` before filling to draw the right side of the bar. – mhawke Oct 03 '17 at 04:01
  • One problem, one problem to go: How can I make the appearance of four color bars as an output? – Anton O. Obyedkov Oct 03 '17 at 17:40
  • @AntonO.Obyedkov: you need to draw more bars. Try `for value in y_values*2:` – mhawke Oct 04 '17 at 00:15