Python Forum
For loops help, using python turtle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loops help, using python turtle
#3
(Mar-10-2020, 04:20 AM)deanhystad Wrote: To draw 5 stick figure you could do something like this:
def main():
    """Draw stick figures, side by side"""
    turtle.pensize(4)
    turtle.title("Stick Figures")
    turtle.up()
    turtle.back(200)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    turtle.done()
This draws five stick figures. What instructions were repeated five times? Those instructions are what you want to execute in the loop, nothing more and nothing less.

Then next thing is figure out how to write a loop. You have a loop in you code that would draw an additional two stick figures if you hadn't already called turtle.done(). How could you make that loop execute 5 times?

the code i have, i should have put that the for loop in myc ode doesnt work as i was trying to test it myself.

def main():
    """Draw the two stick figures, side by side"""
    init()
    draw_stick_figure()     # left stick figure
    turtle.forward(100)
    draw_stick_figure()     # right stick figure
    turtle.hideturtle()
    turtle.done()

    for i in range(1):
        init()
        draw_stick_figure()     # left stick figure
        turtle.forward(200)
        draw_stick_figure()     # right stick figure
        turtle.hideturtle()
        turtle.done()

main()
the forloop used here, doesnt work as i was testing it for myself before i asked for help.

so without the for loop my code would be:

def main():
    """Draw the two stick figures, side by side"""
    init()
    draw_stick_figure()     # left stick figure
    turtle.forward(100)
    draw_stick_figure()     # right stick figure
    turtle.hideturtle()
    turtle.done()

main()

(Mar-10-2020, 04:20 AM)deanhystad Wrote: To draw 5 stick figure you could do something like this:
def main():
    """Draw stick figures, side by side"""
    turtle.pensize(4)
    turtle.title("Stick Figures")
    turtle.up()
    turtle.back(200)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    draw_stick_figure()
    turtle.forward(100)
    
    turtle.done()
This draws five stick figures. What instructions were repeated five times? Those instructions are what you want to execute in the loop, nothing more and nothing less.

Then next thing is figure out how to write a loop. You have a loop in you code that would draw an additional two stick figures if you hadn't already called turtle.done(). How could you make that loop execute 5 times?
would the for loop go along the lines of:
for i in range(5)
    draw_stick_figure()
    turtle.forward(100)
    
    turtle.done()

seems like i have corrected my problem

the solution:

def main():
    """Draw the two stick figures, side by side"""
    turtle.pensize(4)
    turtle.title("Stick Figures")
    turtle.up()
    turtle.back(200)

    for i in range(5):
        draw_stick_figure()
        turtle.forward(100)

main()
Thank you for helping
Reply


Messages In This Thread
RE: For loops help, using python turtle - by SemiBeginnerPY - Mar-10-2020, 10:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why both loops don't work in python? nau 3 1,124 Sep-21-2022, 02:17 PM
Last Post: rob101
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,505 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  Python 3 Turtle - Screen Events and Coords peteralien 0 1,701 Aug-18-2020, 11:25 PM
Last Post: peteralien
  Python for loops Kristenl2784 3 51,271 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  Turtle python graphics Y0sh1 6 3,474 Jun-10-2020, 10:05 AM
Last Post: DPaul
  For loops in python Boinbo 3 84,316 Apr-18-2020, 01:23 AM
Last Post: buran
  Python Turtle Help codinghero 1 16,877 Jan-04-2020, 12:46 AM
Last Post: ichabod801
  Python Turtle and order of implementation query Parsleigh 2 2,794 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,202 Feb-06-2019, 01:25 AM
Last Post: woooee
  Help! Turtle not working, even when we click the turtle demo in IDLE nothing happens. BertyBee 3 5,661 Jan-04-2019, 02:44 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020