CS 115 Lab 3, Part D: Draw a stack of circles using a for-loop
[Back to lab instructions]
Overview
In this part, you will start with a new program and use a for-loop to generate the same image as the one that you just
finished.
Then, instead of always drawing 5 circles, you will ask the user for the number of circles to draw.
Instructions
- Copy and paste the following program
in the
online Python 3 tutor and run it.
x = 100
y = 100
num_circles = 5
radius = 50
for i in range(num_circles):
print('x =', x, 'and y =', y)
# change the value of y for the next circle
- Run this program, and answer questions 15 through 17 in your Moodle writeup.
- Using what you learned in questions 15 though 17, replace the commented line with an appropriate
statement that modifies the value of y so that your loop prints the y-coordinates you
identified in question 14.
- Test your code in the Python tutor, and make sure that it prints the exact values from question 14.
- In PyCharm, create a new Python file lab03d.py. Copy-paste the following program into the new window, substituting your name for the italicized text:
"""
Program: CS 115 Lab 3 Part D
Author: Your name
Description: Using the graphics package, this program will draw a number of
circles using a for-loop.
"""
from graphics import *
def main():
window = GraphWin("Circles", 800, 800)
x = 100
y = 100
num_circles = 5
radius = 50
for i in range(num_circles):
print('x =', x, 'and y =', y)
# copy and paste a code-segment that draws one circle from your
# previous program here.
# copy the line that modifies y-value from Python tutor here
window.getMouse()
window.close()
main()
- Where indicated by the comment, add the line of code that you just wrote in the Python tutor.
- Run the program. Make sure that its output in the console (text) window is still the same as that of the
Python Tutor. It will also pop up an empty graphics window.
- Just above the line you added, replace the other comment with code to draw one circle, based on your code from Part C. Hints:
- Don't copy the code that sets the radius or the x- and y-coordinates.
- The circles can all be the same color.
- You should be sure this code is indented inside the loop, so that it runs 5 times and draws 5 circles.
- Run your program and verify that it draws 5 stacked circles, just like in Part C.
- Replace the line
num_circles = 5
with a line that gets the value of num_circles from the user. You can assume that the
user will always enter a positive integer that is small enough to fit the circles on the screen.
- Test your program, and verify that the stack of circles matches the number specified by the user.
- Replace the line
radius = 50
with a line that gets the value of radius from the user. You can assume that the user
always enters a reasonable positive integer.
- Modify your calculations so that the circles are stacked right on top of each other. You don't have
to change the initial value of y, but you do have to change the way it is adjusted inside
the loop.
- Test your code with several different numbers of circles and radii. This part of the lab isn't
a graded demo, but you should feel free to get a member of the course staff to test it –
you'll build on it in Part E.
- Once you're confident in your Part D code, continue to Part E.