[Python] Can anyone explain me Python for loop (or iteration) algorithm

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I am learning python from Coursera . I have written one program , According to that when I click on the screen , it draws circle . See the program below --

    # Dots

    # importing
    import simplegui
    import math

    width = 600
    height = 600
    ball_list = []
    radius = 20
    colour = "Yellow"

    # position ditector
    def distance(p,q) :
    return math.sqrt((p[0]-q[0])**2 + (p[1]-q[1])**2)



    # Mouse click -- Change the position
    def click(pos) :
    ball_list.append(pos)
    # global position
    # global colour
    # if distance(pos, position) < radius :
    # colour = "Blue"
    # else :
    # position = list(pos)
    # colour = "Yellow"


    # Drawing the ball
    def draw(canvas) :
    for position in ball_list :
    canvas.draw_circle(position , radius , 2, "Black" , colour)

    # Creating the frame
    frame = simplegui.create_frame("Dots" , 600,600)
    frame.set_canvas_background("White")

    frame.set_draw_handler(draw)

    # mouse click
    frame.set_mouseclick_handler(click)


    # Start
    frame.start()


    But my doubt is in def draw(canvas), for position in ball_list , I have not defined any position . I made position = list(pos) as comment . Then what is the value of position in position in ball_list , How can for loop work without any value ? What is iteration ? What is the difference between for loop and iteration ?

    If above code doesn't work in your IDE, please go to http://www.codeskulptor.org/#user38_VSZ0jZ0uTh_0.py and run it .

    Continue reading...

Compartilhe esta Página