Hi i was just wondering if anybody could help me with this code.

#! /usr/bin/env python
# -*- coding: UTF-8 -*-

import turtle
def initialize ( color = "blue" , smallest = 1.0 ) :
    turtle.clear ( )
    turtle.up ( )
    turtle.goto ( -100.0 , 100.0 )
    turtle.setheading ( 0 )
    turtle.color ( color )
    turtle.down ( )
global smallestLineLength
smallestLineLength = "smallest"
def TornSquare ( overallLength ) :
    halfSideLength = ( 0.9 * overallLength ) / 2.0
if 'halfSideLength' <= smallestLineLength :
    moveFunction = turtle.forward
else :
    moveFunction = cesaroLine
a = 85
b = 180 - 2 * a
moveFunction ( 'halfSideLength' )
turtle.right ( a )
moveFunction ( 'halfSideLength' )
turtle.left ( 180 - b )
moveFunction ( 'halfSideLength' )
turtle.right ( a )
moveFunction ( 'halfSideLength' )

def TornSquareFractal (overall , smallest = 1.0 , color = "blue")  :   
    cesaroLine ( overall )
    turtle.right ( 90 )
    cesaroLine ( overall )
    turtle.right ( 90 )
    cesaroLine ( overall )
    turtle.right ( 90 )
    cesaroLine ( overall )

if __name__ == "__main__" :
    #initialize ( ’brown’ , 100 ) ; cesaroLine ( 200 )
    TornSquareFractal ( 200.0 , 10.0 , "brown" )
    raw_input ( "Press enter to end" )

Everytime i run it i get this error:


Traceback (most recent call last):
File "C:/Python27/torn square.py", line 21, in <module>
moveFunction ( 'halfSideLength' )
File "<string>", line 1, in forward
File "C:\Python27\lib\lib-tk\turtle.py", line 1554, in forward
self._go(distance)
File "C:\Python27\lib\lib-tk\turtle.py", line 1521, in _go
ende = self._position + self._orient * distance
File "C:\Python27\lib\lib-tk\turtle.py", line 277, in __mul__
return Vec2D(self[0]*other, self[1]*other)
TypeError: can't multiply sequence by non-int of type 'float'

Can anybody help me see where i am going wrong?

Recommended Answers

All 13 Replies

Try making 'halfSideLength' an int.
ie line 21:

alfSideLength = int(( 0.9 * overallLength ) / 2.0)

Just a hunch, seeing as the error says it can't "multiply not-int type float()", when you call the function on the line specified.

Thanks for the reply but it didn't work. I still get the same error.

Hi, look

on line 21-25

moveFunction is not a function but a variable.

why are you calling

moveFunction ("bla bla")

instead of

moveFunction ="bla bla"

Fix that ok ;)

commented: Very helpful +1

Thanks for the reply, the error has now gone but i now have another problem lol. Can anybody tell me why it doesn't draw? Is it because of the code or is it a problem with my laptop because everytime i run it the arrow moves but then Python crashes. Again any help would be very much appreciated.

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import turtle
def initialize ( color = "blue" , smallest = 1.0 ) :
    turtle.clear ( )
    turtle.up ( )
    turtle.goto ( -100.0 , 100.0 )
    turtle.setheading ( 0 )
    turtle.colour ( colour )
    turtle.down ( )
global smallestLineLength
smallestLineLength = "smallest"
def TornSquare ( overallLength ) :
    halfSideLength = int (( 0.9 * overallLength ) / 2.0)
if 'halfSideLength' <= smallestLineLength :
    moveFunction = turtle.forward
else :
    moveFunction = TornSquare
a = 85
b = 180 - 2 * a
moveFunction = 'halfSideLength'
turtle.right ( a )
moveFunction = 'halfSideLength'
turtle.left ( 180 - b )
moveFunction = 'halfSideLength'
turtle.right ( a )
moveFunction = 'halfSideLength'

def TornSquareFractal (overall , smallest = 1.0 , color = "blue")  :   
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )

if __name__ == "__main__" :
    #initialize ( ’brown’ , 100 ) ; cesaroLine ( 200 )
    TornSquareFractal ( 200.0 , 10.0 , "brown" )
    raw_input ( "Press enter to end" )

number 12 and 15.

You made a comparism to

#
smallestLineLength = "smallest" ## this is a string

def TornSquare ( overallLength ) :

halfSideLength = int (( 0.9 * overallLength ) / 2.0)

if 'halfSideLength' <= smallestLineLength : # you are comparing a string.... No good for you here. I must be some sort of int data/float to determine movement. As far i am concern, this comparism is useless.

Keep refactoring ok? ;)

Run this code

#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import turtle
def initialize ( color = "blue" , smallest = 1.0 ) :
    turtle.clear ( )
    turtle.up ( )
    turtle.goto ( -100.0 , 100.0 )
    turtle.setheading ( 0 )
    turtle.colour ( colour )
    turtle.down ( )
global smallestLineLength
smallestLineLength = 90
def TornSquare ( overallLength ) :
    halfSideLength = int (( 0.9 * overallLength ) / 2.0)
    if  halfSideLength <= smallestLineLength :
       moveFunction = turtle.forward(100)
    else :
      moveFunction = TornSquare
    a = 85
    b = 180 - 2 * a
    moveFunction = halfSideLength
    turtle.right ( a )
    moveFunction = halfSideLength
    turtle.left ( 180 - b )
    moveFunction = halfSideLength
    turtle.right ( a )
    moveFunction = halfSideLength

def TornSquareFractal (overall , smallest = 1.0 , color = "blue")  :   
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )
    turtle.right ( 90 )
    TornSquare ( overall )

if __name__ == "__main__" :
    #initialize ( ’brown’ , 100 ) ; cesaroLine ( 200 )
    TornSquareFractal ( 200.0 , 10.0 , "brown" )
    raw_input ( "Press enter to end" )

And dont forget to upvote my a lil more ok ;)
Your initialize method up there is useless for now ;)

Richieking again thank you for your help but it only draws a square and not a torn square.

what do you mean by torn square? like this?

++++++++++
+        +      
+        +
+        +
++++++++++

more info pls ;)

You cant do this with turtule module my friend.

you need these modules.
1. math
2. PIL
or pygame i think.

You are using the wrong tool for the wrong job brother ;)

Richieking thank you so uch for all of your help! I have finally done it :)

can you post the code. i am curious...... plz ;)

You are not entering to the mainloop. Can you explain the purpose of 21 onwards setting string again to value it has allready?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.