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
#1
Hi everyone, im still a beginner for PY.

recently i have been trying to create stickman in Python using turtle. i dont want to use multiple methods to create all the stickman. so ive thought of adding a loop to my code. ill put the code below for you guys to look at and help me identify how i can solve this problem.
my code for the stickman drawing:

def init():
    turtle.pensize(4)
    turtle.title("Stick Figures")
    turtle.up()
    

def draw_torso():
    turtle.color('blue')
    turtle.right(90)
    turtle.down()
    turtle.forward(100)
    turtle.up()
    turtle.back(100)
    turtle.left(90)

def draw_legs():
    turtle.color('green')
    turtle.right(90)
    turtle.forward(100)
    turtle.right(45)
    turtle.down()
    turtle.forward(30)
    turtle.back(30)
    turtle.left(90)
    turtle.forward(30)
    turtle.back(30)
    turtle.right(45)
    turtle.up()
    turtle.back(100)
    turtle.left(90)

def draw_arms():
    """Draw the arms"""
    turtle.color('yellow')
    turtle.right(90)
    turtle.forward(30)
    turtle.left(90)
    turtle.down()
    turtle.forward(30)
    turtle.back(30)
    turtle.left(180)
    turtle.forward(30)
    turtle.back(30)
    turtle.left(90)
    turtle.up()
    turtle.back(30)
    turtle.left(90)

def draw_head():
    """Draw the head"""
    turtle.color('purple')
    turtle.down()
    turtle.circle(25)
    turtle.up()

def draw_stick_figure():
    """Draw a complete single stick figure"""
    draw_torso()            # task 1 - draw the torso
    draw_legs()             # task 2 - draw the legs
    draw_arms()             # task 3 - draw the arms
    draw_head()             # task 4 - draw the head

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 code above depicts how ive made two stickman side by side, however i want to change that now by adding a for loop where the turtle will keep on creating stickman by using my loop. i want to create atleast 5 stickman side by side.


Any help would be appreciated.

**before anyone asks i have read and researched about using loops but i cannot find a way to input the for loop into my code for this to work.

i forgot to add my code before this and what i am creating:

import turtle
import time
t = turtle.Turtle()
scale = 25
#Task 2.1
def initials():
## Letter A
    t.penup()
    t.setx(0)
    t.sety(0)
    t.down()
# Point upwards to begin
    t.left ( t.heading ( ) + 90 )
    t.right ( 20 )
    t.forward ( 10 * scale )
    t.right ( 70 )
    t.forward ( 1 * scale )
    t.right ( 70 )
    t.forward ( 10 * scale )
    t.backward ( 5 * scale )
    t.right ( 90 + 20 )
    t.forward ( 5 * scale )
    t.up ( )
    t.backward ( 5 * scale )
    t.left ( 110 )
    t.forward ( 5 * scale )
    t.left ( 70 )
    t.forward ( 1 * scale )
    t.setx(200)
    t.sety(10)
    t.pendown()
## Ltter W
    t.right(90)
    t.forward(50)
    t.left(150)
    t.forward(40)
    t.right(120)
    t.forward(40)
    t.left(150)
    t.forward(50)
    t.penup()
    t.right(90)
    t.forward(50)

initials()
t.hideturtle()
t.clear()  
the code above makes my initials as you can see i have commented on each line so this should make more sense for anyone wondering what/why i am coding this. the code above is inserted before the stickman code.
Reply
#2
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?
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why both loops don't work in python? nau 3 1,055 Sep-21-2022, 02:17 PM
Last Post: rob101
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,399 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  Python 3 Turtle - Screen Events and Coords peteralien 0 1,652 Aug-18-2020, 11:25 PM
Last Post: peteralien
  Python for loops Kristenl2784 3 41,865 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  Turtle python graphics Y0sh1 6 3,382 Jun-10-2020, 10:05 AM
Last Post: DPaul
  For loops in python Boinbo 3 68,501 Apr-18-2020, 01:23 AM
Last Post: buran
  Python Turtle Help codinghero 1 13,385 Jan-04-2020, 12:46 AM
Last Post: ichabod801
  Python Turtle and order of implementation query Parsleigh 2 2,707 Mar-04-2019, 02:43 PM
Last Post: Parsleigh
  wn = turtle.screen() AttributeError: module 'turtle' has no attribute 'screen' Shadower 1 6,127 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,540 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