-1

I'm working on a project to draw a Sierpinksi triangle in Python Turtle. I'm having some issues with it. When I run the code the window immediately shuts down. When I run the code I get this message: TypeError: can't multiply sequence by non-int of type 'float'. I'm trying to understand what is causing this error message, but I'm not that skilled yet to see it.

Can somebody help me out? I just started learning python...

from hashlib import new
from math import sqrt
from turtle import *
import turtle

turtle = turtle.Turtle()
size = 100
colormap = ['blue','red','green','white','yellow', 'violet','orange']

def get_height():
    return 1/2 * size * sqrt(3)

def drawTriangle(points, color, turtle):
    turtle.fillcolor(color)
    turtle.up()
    turtle.goto(points[0][0],points[0][1])
    turtle.down()
    turtle.begin_fill()
    turtle.goto(points[1][0],points[1][1])
    turtle.goto(points[2][0],points[2][1])
    turtle.goto(points[0][0],points[0][1])
    turtle.end_fill()

def sierpinski(left_under_point, n, turtle):
    print (left_under_point)
    points = [
        left_under_point,
        [left_under_point[0]+size, left_under_point[1]],
        [left_under_point[0]/2, left_under_point[1]+get_height()]
    ]
    
    drawTriangle(points, colormap[n], turtle)
   
    if n == 0 :
        begin_fill()
        forward (points)
        left (120)
        forward (points)
        left (120)
        forward (points)
        left (120)
        end_fill()

    else :
        new_left_under_point = [
            left_under_point[0] - (size/4),
            get_height() / 2
        ]
        sierpinski (new_left_under_point, n-1, turtle)

def main():
    speed(10)
    setup(700, 700)
    title ("Sierpinski Driehoek")
    cordinates = [-100,-50]
    rang = 3
    #rang = int(numinput("Sierpinski Driekhoek", "Welke grootte (N)?", rang, minval=0, maxval=5))
    sierpinski(cordinates, rang, turtle)
    turtle.exitonclick()
   
main()
L Bergsma
  • 1
  • 2
  • I get an error when running this script: `TypeError: can't multiply sequence by non-int of type 'float'`. It may be the reason why the script ends – Tryph Jan 25 '22 at 10:15
  • I do indeed see the same message when I run the code. I can't quite place what's causing the problem... As I mentioned, I'm pretty new to python – L Bergsma Jan 25 '22 at 10:21
  • The best I can do (unless trying to understand what you tried in your code and fix it) is pointing you to docs about error message understanding: see https://realpython.com/python-traceback/ and https://www.sentinelone.com/blog/python-stack-trace-understanding-using-debug/ – Tryph Jan 25 '22 at 10:26
  • Okay, I will first look at the error message understanding. Thanks! – L Bergsma Jan 25 '22 at 10:45

2 Answers2

0

You can use "turtle.bye()" for this.

Usually, a lack of turtle.mainloop(), or one of its variants, will cause the window to close because the program will exit, closing everything. turtle.mainloop() should be the last statement executed in a turtle graphics program unless the script is run from within Python IDLE -n which disables turtle.mainloop() and variants.

  • Thanks for your help Maulik! Just used turtle.bye(), but the screen is still shutting down.. Is there something about my code that keeps the screen shutting down? – L Bergsma Jan 25 '22 at 10:19
  • 90% of this answer was taken from [this answer](https://stackoverflow.com/a/42283059/5771269) but you failed to credit, link or up vote that answer! And I don't see how it explains the OP's problem which is an incorrect data type passed as an argument to turtle's `forward()` method. – cdlane Jan 25 '22 at 23:12
0

As the error message indicates:

File "test.py", line 36, in sierpinski forward (points) ... TypeError: can't multiply sequence by non-int of type 'float'

In line 36, when you do forward (points), points is a list of three floating point positions:

points = [
    left_under_point,
    [left_under_point[0]+SIZE, left_under_point[1]],
    [left_under_point[0]/2, left_under_point[1]+get_height()]
]

but what forward() wants is a simple number, like 125. Put a print(points) statement before this line and you'll see the argument that forward() is complaining about.

cdlane
  • 40,441
  • 5
  • 32
  • 81