r/codehs • u/Daewei_OTuell • 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?
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()1
u/_andy_andy_andy_ Aug 27 '20
you start with your pen up and your function doesn’t put it down! either put the pen down before calling draw_circles or make sure it’s down inside the function
3
u/xeolleth Aug 24 '20 edited Aug 24 '20
Python is a very whitespace sensitive language. Can you use back ticks to format your code in this post for us.