Flag Creator

flags

Welcome to the first guided project of PyFlo. The goal of these projects is to apply what you’ve learned so far by writing some code to solve a problem, or group of problems. At this point in the flow, we’ve only discussed a limited number of Python topics, so your ability to solve something truly “interesting” at this point is somewhat limited. However, you can use your skills with variables, strings, math, and string multiplication to solve the series of problems in this assignment.

In this guided project, your task is to write a program that can print out flags that resemble the U.S. flag. The flag for the United states of America has alternating stripes with a blue canton containing 50 stars within. Though the U.S. has it’s own unique version, this general style of flag (striped flag with solid canton) is not unique to just the U.S. Some other states and countries share flags with a similar style, including Sao Paulo and Uruguay. Notice that though each has its own details, they all follow the same design principle.

The program you are tasked with writing must have the ability to print out flags of this pattern. The program should accept two input values from the user - the desired width and height of the flag to create. Then, the program should print out a flag in which the top-left corner is solid (with # characters) and the rest shows stripes with the dash (-) character. Included below are two example of what this program should behave like when completed:

Flag width:
10
Flag height:
4
#####-----
#####-----
----------
----------
Flag width:
20
Flag height:
8
##########----------
##########----------
##########----------
##########----------
--------------------
--------------------
--------------------
--------------------

Notice how the width of the flag is determined by the first input and the height by the second. Also notice that the size of the canton in the corner needs to change, based on the total size of the flag. Some math will be needed for this! This problem can be a bit intimidating to try to tackle all at one, so solving it will be broken down into three phases. For the first phase, you write a program that just prints out one, fixed-size, 10 by 4 flag. It should not ask for any input values - just print the flag that looks exactly like this:

#####-----
#####-----
----------
----------
Implement the code to print out the 10x4 flag.
This should take no more than 4 lines of code.
# Write your code to print out the flag here! # Click the PLAY button to run your code # Click the CHECK button to test your code

If you got the test case above to pass, congratulations! You’ve taken the first step to getting this project completed.

Next up you should add the ability for the program to ask for one input value from the user, that being the width of the flag. We won’t worry about adjusting the height just yet. Sticking to solving one component of the problem at a time is better than trying to tackle it all at once. To do this, you should change the program to:

Implement the code to print out a flag that can vary in width only. width = input("Flag width:\n") width = int(width) # Put the rest of your code here!

After you’ve gotten all of the test cases for the width-only flag printer passed, it’s time to move on to the final stage. The last step is to get both the width and height inputs, and print out a flag that can dynamically scale in both directions. In order to do this, you’ll need to calculate what half of the width and half of the height are, so that you can know how to print out the canton corner of the flag sized properly.

Implement the code to print out a flag that can vary in both width and height.
It should behave the same way that the two examples earlier in this project showed.
width = input("Flag width:\n") width = int(width) height = input("Flag height:\n") height = int(height) # Put the rest of your code here!

If you passed all of the test cases, then congratulations. You’ve applied your Python programming skills to build something cool. The complete solution is available for download as a .py file at the link below.

Solution Code



PyFlo Home Complete Bookmark Next