Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

python - Flowchart nested for-loop

I have this code and I tried to do a flowchart but I have no clue how to make one. All of my Flowchart I made, don't make any sense.

Could anyone of you guys please help me out???

import turtle

STARTING_X, STARTING_Y = 350, 200

turtle.penup()
turtle.width(2)
turtle.setheading(180)
turtle.sety(STARTING_Y)

for a in range(1, 8):
    turtle.penup()
    turtle.setx(STARTING_X)

    for b in range(a):
        turtle.pendown()
        turtle.circle(25)
        turtle.penup()
        turtle.forward(60)

    turtle.sety(turtle.ycor() - 60)

turtle.done()
question from:https://stackoverflow.com/questions/65643074/flowchart-nested-for-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The code:

# part 1
import turtle
STARTING_X, STARTING_Y = 350, 200
turtle.penup()
turtle.width(2)
turtle.setheading(180)
turtle.sety(STARTING_Y)

# part 2
for a in range(1, 8):
    turtle.penup()
    turtle.setx(STARTING_X)

    for b in range(a):    # part 3
        turtle.pendown()
        turtle.circle(25)
        turtle.penup()
        turtle.forward(60)

    turtle.sety(turtle.ycor() - 60)

turtle.done()

Part 1:

  • Importing turtle
  • Making global variables for starting positions
  • turtle stuff (read docs)

Part 2:

  • for loop that runs 8 times
  • turtle pen up (not drawing)
  • start at predefined x position
  • for loop (see Part 3)
  • lower the y position with 60

Part 3:

  • runs a times

  • turtle pendown (drawing)

  • drawing a circle

  • turtle penup (not drawing)

  • go forward with 60 (in this case, lower the x position with 60 because of the direction in part 1)


**Summary** This program draws 8 lines of circles, and each nth line contains n circles aligned to the right like this:
        *
       **  
      *** 
     **** 
    ***** 
   ****** 
  ******* 
 ******** 

But circles instead of stars


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...