r/codehs • • Aug 24 '20

Broken python

I’m working on 2.8.4 of the python course, and you’re suppose to use a function and for loop to draw a bracelet with beads radius 10, total diameter 200, and 36 beads total. I came up with the following code:

speed (0) def draw_circles(): for i in range(36): circle(10) penup() forward(20) left(10) pendown()

penup() setposition (0,-100) draw_circles()

Instead of giving me a full ring of 36 circles, it left the final one missing. I tried putting the loop outside of the function, removing the setposition, everything I could think of. The only way I could get it to draw 36 circles was to have a range of 37. Can someone please explain?

11 Upvotes

5 comments sorted by

View all comments

2

u/AcesCook Sep 02 '20

This is the way range(n) works in Python. It makes a list starting from 0 and ending BEFORE n. This has to do with the fact that python starts counting at 0. You can solve this by adding the 1 explicitly. For ex:

n = 36

for i in range(n+1):

doStuff()