Writing the For Loop correctly in Python

1948 views python
6

When I tried to make a set of pretty little lines on a canvas in Python, I could not get my For Loop to run correctly. I intended for my For Loop to work similarly to what the commented out bit of code has done. Unfortunately, the code just didn't work at all and only caused an error message.

What I wanted to do with that For Loop was to make the variable "linecounter" to go up 20 in value 20 times, and then make the x-coordinates of the lines increase by 50 pixels to the right each time. With my current code right now, it just simply doesn't work.

Is there something in my For Loop that isn't quite written right?

import tkinter as tk

root = tk.Tk()
root.geometry("960x600")

canvas = tk.Canvas(width=960, height=900, bg='white')
canvas.grid(row=2, column=0, columnspan=3)

##canvas.create_line(0, 50, 0, 100, width=5, fill="black")
##canvas.create_line(50, 50, 50, 100, width=5, fill="black")
##canvas.create_line(100, 50, 100, 100, width=5, fill="black")
##canvas.create_line(150, 50, 150, 100, width=5, fill="black")
##canvas.create_line(200, 50, 200, 100, width=5, fill="black")
##canvas.create_line(250, 50, 250, 100, width=5, fill="black")
##canvas.create_line(300, 50, 300, 100, width=5, fill="black")
##canvas.create_line(350, 50, 350, 100, width=5, fill="black")
##canvas.create_line(400, 50, 400, 100, width=5, fill="black")
##canvas.create_line(450, 50, 450, 100, width=5, fill="black")
##canvas.create_line(500, 50, 500, 100, width=5, fill="black")
##canvas.create_line(550, 50, 550, 100, width=5, fill="black")
##canvas.create_line(600, 50, 600, 100, width=5, fill="black")
##canvas.create_line(650, 50, 650, 100, width=5, fill="black")
##canvas.create_line(700, 50, 700, 100, width=5, fill="black")
##canvas.create_line(750, 50, 750, 100, width=5, fill="black")
##canvas.create_line(800, 50, 800, 100, width=5, fill="black")
##canvas.create_line(850, 50, 850, 100, width=5, fill="black")
##canvas.create_line(900, 50, 900, 100, width=5, fill="black")
##canvas.create_line(950, 50, 950, 100, width=5, fill="black")
##canvas.create_line(1000, 50, 1000, 100, width=5, fill="black")

for linecounter 1 to 20
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

root.mainloop()

answered question

please be sure to properly research your issues before you post questions here.

2 Answers

10

for linecounter in range(21):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

posted this
0

The information you need to fix your problem can be found at: https://wiki.python.org/moin/ForLoop

You should utilize Google to fix simple problems like this. You narrowed your issue down to the for-loop, and searching for "For Loops Python" could have shown you the proper way to do this.

Your for-loop needs to be rewritten as:

for linecounter in range(20):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

This will iterate from 0 to 19. To iterate from 0 to 20, which it appears you're trying to do, use:

for linecounter in range(21):
    canvas.create_line((linecounter * 50), 50, (linecounter * 50), 100, width=5, fill="black")

posted this

Have an answer?

JD

Please login first before posting an answer.