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

  1. 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
      
  2. Run this program, and answer questions 5 through 7 in your Moodle writeup.
  3. Using what you learned in questions 5 though 7, 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 4.
  4. Test your code in the Python tutor, and make sure that it prints the exact values from question 4.
  5. In IDLE, go to the File menu and select "New File." 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()
  6. Where indicated by the comment, add the line of code that you just wrote in the Python tutor.
  7. Save this program as lab03d.py.
  8. 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.
  9. Just above the line you added, replace the other comment with code to draw one circle, based on your code from Part C. Hints:
  10. Run your program and verify that it draws 5 stacked circles, just like in Part C.
  11. 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.
  12. Test your program, and verify that the stack of circles matches the number specified by the user.
  13. 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.
  14. 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.
  15. 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.
  16. Once you're confident in your Part D code, continue to Part E.