Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

NEED HELP WITH TURTLE FUNCTION!! URGENT

kevinlaikevinlai Posts: 2Member
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). The original rectangles are in blue. then the program will do the sorting for every pass, 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 is some code I have come up with so far:

def drawAction(lst, width):
for height in lst:
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.pencolor("")
t.forward(30)
t.pencolor("black")
t.fill(False)


def drawAction_color(fillcolor):
t.fillcolor(fillcolor)
t.fill(True)


def swap(lst):
length = len(lst)
for i in range(0, length):
for n in range(0, length-i-1):
if lst[i] > lst[i+1]:
lst[i], lst[i+1] = lst[i+1], lst[i]
break

return lst


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

Sign In or Register to comment.