F01.justanswer.com



CS 115 Lab 10, Part D: Refactoring: national flags[Back to Lab 10] [Back to CS 115 schedule]SetupBecause this part of the lab is graphical, you will need to do it on your local machine. Follow these instructions to get set up.Right-click on this link:?. Select "Save Link As..." or "Save Target As..." If you are using Mac OS, save the file into your Documents directory. If you are on Windows, save it into C:\Python32 or C:\Python33.In a separate browser tab, open?the reference document for the graphics library, just in case you need to refer to it during this lab.InstructionsIn IDLE, create the following program and save it as lab10d.py, substituting your name for the?italicized text:"""Program: CS 115 Lab 10dAuthor: Your nameDescription: This program displays national flags"""from graphics import *def draw_france_flag(width): ''' Draws national flag of France given the width in pixels. ''' height = 2 / 3 * width stripe_width = 1 / 3 * width # Open a new graphics window with the title 'French flag' # and a size of width x height pixels win = GraphWin('French flag', width, height) # Set the window background to white win.setBackground('white') # We need to draw a blue stripe at the left of the flag blue_top_left = Point(0, 0) # the top left corner of the flag blue_bottom_right = Point(0 + stripe_width, height) blue = Rectangle(blue_top_left, blue_bottom_right) blue.setFill('DarkBlue') blue.setOutline('DarkBlue') blue.draw(win) # ...and a red stripe at the right red_top_left = Point(2 * stripe_width, 0) red_bottom_right = Point(width, height) # the bottom right corner of the flag red = Rectangle(red_top_left, red_bottom_right) red.setFill('red') red.setOutline('red') red.draw(win) # Close? print('Click in the flag window to close it.') win.getMouse() win.close()# --- Our main function ---def main(): flag_width = int(input('Enter desired flag width: ')) draw_france_flag(flag_width)main()Add the following function definition to your program. This function "factors out" the repetitive code for drawing the two stripes.def draw_rectangle(window, top_left, bottom_right, color): ''' Draws a rectangle and returns the Rectangle object Parameters: - window: the window to draw in - top_left: the coordinates of the top left corner (as a Point) - bottom_right: the coordinates of the bottom right corner (as a Point) - color: the color to make the rectangle (as a string) Returns: the Rectangle that was drawn ''' rectangle = Rectangle(top_left, bottom_right) rectangle.setFill(color) rectangle.setOutline(color) rectangle.draw(window) return rectangleModify your?draw_france_flag?function so that it replaces 4 of the lines beginning with?blue?with a single call to?draw_rectangle. Test your program, and then perform a similar replacement on the lines beginning with?red. Test your program to be sure it still works.Many national flags follow the general pattern of having some number of horizontal or vertical stripes of equal size. Next, we will attempt to write a general function to draw stripes on a flag. Start with the following function definition:def draw_stripes(window, width, height, stripe_colors, horiz): ''' Draws equal-sized stripes in a graphical window. Parameters: - the graphical window - the window's width and height (as integers) - the colors of the stripes (as a list of strings) - horiz: True if the stripes are horizontal, False if they are vertical ''' passNotice that this function expects its caller to pass it 5 parameters. Read their descriptions carefully. Note that the fourth parameter will be a list. For example:?['red', 'white']Replace the?pass?in?draw_stripes?with a line of code that finds the number of colors (that is, the length) in the list that was passed to this function, and saves that value to a variable.Add a line of code to loop over the list of colors. You should either use?enumerate?or?range, because you will need to know the index of each element. DON'T just do?for c in stripe_colors!Within your loop, you will handle the horizontal and vertical cases separately. Add the following lines:if horiz: # equivalent to if horiz == True: passelse: # if the stripes are vertical passLet's focus on the horizontal case. Consider the flag of Indonesia:Answer Question 5 in your writeup.Now consider the flag of Armenia:Answer Question 6 in your writeup.Inside the?if horiz:?statement, add code to create?Point?variables for the top left and bottom right of stripe?i. Be sure to use your length variable instead of assuming a fixed number of stripes, and use?i?to talk about the current stripe. You may need to get out some paper to figure out a formula relating?i, the number of stripes, and the height of the flag to your desired coordinates. For example:stripe_top_left = Point(...)stripe_bottom_right = Point(...)Then add a call to your new?draw_rectangle?function to pass it your desired color and coordinates.Figure out a similar formula for vertical stripes, and add it to the?else?case.Finally, replace your?draw_france_flag()?function with the following code:def draw_france_flag(width): ''' Draws national flag of France given the width in pixels. ''' height = 2 / 3 * width # Open a new graphics window with the title 'French flag' # and a size of width x height pixels win = GraphWin('French flag', width, height) draw_stripes(win, width, height, ['DarkBlue', 'white', 'red'], False) print('Click in the flag window to close it.') win.getMouse() win.close()Write a function to draw the Russian flag using your new functions. This code should be very similar to the?draw_france_flag?function It should also be approximately 6 lines of code.Add a line to?main?to call your Russian flag function. Verify that your program draws the French flag followed by the Russian flag:Demo your program for a member of the course staff.Continue to Part E. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches