0

I'm using turtle to show a sorter. I have the number sorting working fine but I am working on sorting the bars. I would like to know if there's a way to assign the output of a function to a variable each time it is called. More specifically, I want to be able to assign each separate bar to a variable, and then put all the bar variables into a list and sort it simultaneously with the numbers from nums. Hopefully I'm making sense. Any help would be appreciated.

nums=[30,60,90] ##sorted list

draw(): ##draws the bar based on height of number in the list
    t.fd(5)
    t.lt(90)
    t.fd(nums[i])
    t.lt(90)
    t.fd(5)
    t.lt(90)
    t.fd(nums[i])
    t.lt(90)
    t.pu()
    t.fd(50)
    t.pd()

for i in range(len(nums)): ##draws all lines in the list
    draw()

1 Answer 1

1

Do you mean this:

def draw(num): ##draws the bar based on height of number in the list
    t.fd(5)
    t.lt(90)
    t.fd(num)
    t.lt(90)
    t.fd(5)
    t.lt(90)
    t.fd(num)
    t.lt(90)
    t.pu     # typo?
    t.fd(50)
    t.pd()

nums=[30,60,90] ##sorted list

for num in nums:
    draw(num)

If not, please give an example of what you're trying to achieve.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.