turtle functions in python

This is the place for queries that don't fit in any of the other categories.

turtle functions in python

Postby ltcy » Tue Apr 09, 2013 7:04 am

I have some questions about turtle function in python. If I want to bubble sort the list the user has typed and then use the numbers in the list, for example [20, 50, 70] as the height of my graphic(supposed it's a rectangle), what should I type in the turtle function part? Here is my code :
Code: Select all
def drawAction(lst, width):
    t.forward(width)
    t.left(90)
    t.forward(lst)
    t.left(90)
    t.forward(width)
    t.left(90)
    t.forward(lst)

import turtle as t
lst = input("Provide list to be sorted: ")
width = input("Provide width ")
drawAction(lst, width)


However an error appears: TypeError: can't multiply sequence by non-int of type 'float' so can someone please give me any suggestion in editing my code? thanks
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: turtle functions in python

Postby stranac » Tue Apr 09, 2013 9:27 am

For future reference, you should post the entire error traceback, instead of just the last line of it.

Anyway, turtle.forward() takes a number, not a list.
So you'll have to do something like:
Code: Select all
for height in lst:
    # do the movements here


Also, using input() is not recommended.
You should use raw_input() instead, and do the type conversions yourself.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: turtle functions in python

Postby ltcy » Wed Apr 10, 2013 7:06 am

Hi, I have another question, suppose the original rectangles are in blue, if I want to change the color of the swapping rectangle into green, what should I type in my code? where should I add in the code? here're some codes I have come up with in the "top level":

Code: Select all
# TOP LEVEL
import turtle as t
lst = input("Provide list to be sorted: ")
width = input("Provide width ")
for n in range(len(lst)):
    drawAction_color("blue")
    drawAction(lst, width)
    cont = raw_input("hit any key to continue ")
    t.home()
    t.clear()
    swap(lst)
    if lst[i] and lst[i+1] == lst[i+1] and lst[i] :
        drawAction_color("green")
   
drawAction(lst, width)
print "Done! Sorted list is : ", lst



stranac wrote:For future reference, you should post the entire error traceback, instead of just the last line of it.

Anyway, turtle.forward() takes a number, not a list.
So you'll have to do something like:
Code: Select all
for height in lst:
    # do the movements here


Also, using input() is not recommended.
You should use raw_input() instead, and do the type conversions yourself.
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: turtle functions in python

Postby stranac » Wed Apr 10, 2013 9:35 am

ltcy wrote:suppose the original rectangles are in blue, if I want to change the color of the swapping rectangle into green, what should I type in my code? where should I add in the code?


You'll probably want to call the turtle.pencolor() function.
Put it somewhere before the code that draws this swapping rectangle.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: turtle functions in python

Postby ltcy » Wed Apr 10, 2013 10:53 pm

Hi, I tried but it changes all rectangles into green, what if I just want to change the swapping rectangles into green and keep the others in blue?
stranac wrote:
ltcy wrote:suppose the original rectangles are in blue, if I want to change the color of the swapping rectangle into green, what should I type in my code? where should I add in the code?


You'll probably want to call the turtle.pencolor() function.
Put it somewhere before the code that draws this swapping rectangle.
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: turtle functions in python

Postby stranac » Thu Apr 11, 2013 8:23 am

ltcy wrote:I tried but it changes all rectangles into green

It shouldn't change anything.
The rectangles you've drawn before changing the color should keep their color.
So, draw the blue rectangles first, then change the color and draw the green ones.

If you're still having trouble, show the code you're trying to use.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: turtle functions in python

Postby ltcy » Thu Apr 11, 2013 7:00 pm

Code: Select all
# TOP LEVEL
import turtle as t
lst = input("Provide list to be sorted: ")
width = input("Provide width ")
drawAction_color("blue")
for n in range(len(lst)):
    drawAction(lst, width)
    cont = raw_input("hit any key to continue ")
    t.home()
    t.clear()
    drawAction_color("green")
    swap(lst)


Here is my currently code in top level. but the problem is the rectangles starts with blue, but when I press "hit any key to continue", the whole rectangles changes to green, I was wondering if I should add the drawAction_color("green") in the def swap(lst) part?
stranac wrote:
ltcy wrote:I tried but it changes all rectangles into green

It shouldn't change anything.
The rectangles you've drawn before changing the color should keep their color.
So, draw the blue rectangles first, then change the color and draw the green ones.

If you're still having trouble, show the code you're trying to use.
ltcy
 
Posts: 12
Joined: Sat Feb 16, 2013 1:32 am

Re: turtle functions in python

Postby stranac » Thu Apr 11, 2013 7:56 pm

Show your entire code.
It's easier to help when you have code that actually runs.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests