Description
Part 1: Framed Box
In this part, we will solve a framing problem similar to Lab 2.
Write a program that asks the user for the width and height of a framed box, and the character
to use in the frame. Then, display a box of the given size, framed by the given character. Also,
display the dimensions of the box inside the second line of the box (just after the left frame and a
space).
Assume that the user inputs valid values for each input: width is a positive integer (7 or higher)
and height is a positive integer (4 or higher), and a single character is given for the frame.
Here is an expected output of your program (how it will look on the homework submission server):
Width of box ==> 7
Height of box ==> 4
Enter frame character ==> @
Box:
@@@@@@@
@ 7×4 @
@ @
@@@@@@@
Here is another possible output:
Width of box ==> 12
Height of box ==> 8
Enter frame character ==> #
Box:
############
# 12×8 #
# #
# #
# #
# #
# #
############
This is in essence very similar to the lab, except that you will need to put the box dimensions in a
string first, and then use its length to figure out how long the second line should be.
When you have tested your code, please submit it as hw1_part1.py.
Part 2: Madlibs
In this part you will write code to construct a madlib given below:
Look, …
I can see you’re really about this …
I honestly think you ought to calmly …
take a and think things over …
I know I’ve made some very decisions recently,
but I can give you my complete that my work will be back
to .
You will ask the missing pieces of the following madlib to the user using the (raw_input) function
we learnt in class on thursday. For each input, you will ask the specific type of word required.
You will then take all the user specified inputs, and construct the above madlib. You can use any
string method we have learnt. Make sure your output looks like the above paragraph, except that
the missing information is filled in with the user input. Here is an example run of the program
(how it will look at the homework submission server):
proper name ==> Brad
emotion ==> happy
verb ==> eat
adjective ==> silly
noun ==> mouse
adjective ==> hungry
noun ==> computer
adjective ==> cute
Here is your output:
Look, Brad …
I can see you’re really happy about this …
I honestly think you ought to eat calmly …
take a silly mouse and think things over …
I know I’ve made some very hungry decisions recently,
but I can give you my complete computer that my work will be back
to cute.
When you have tested your code and sure that it works, please submit it as hw1_part2.py.
Part 3: Name popularity
How popular were different names in different decades? Which names have increased or decreased
in popularity? We can actually find out thanks to the various real data sets available these days.
This homework will be based on real data, but we only ask you to read the numbers for one name
using raw_input().
Write a program that reads the number of babies born with a specific name in decades: 1970,
1980, 1990 and 2000. You must then print the information using a table like the one below. Then,
compute the percent change in popularity between each consecutive decade. Print this information
and the average percent difference.
You will need to use formatted strings for this to look correct. If you want to print percent sign %
in a formatted string, you need to escape it by writing it twice. Here is an example:
>>> print “The decrease: %% %.2f” %(12.345678)
The decrease: % 12.35
Here is an example output of the program for name Anya (how it will look on the homework
submission server):
Please enter a name ==> Anya
Count in 1970 => 520
Count in 1980 => 1326
Count in 1990 => 1495
Count in 2000 => 6875
Babies named Anya
*****************
Year / Total / % change from previous decade
1970 / 520
1980 / 1326 / % 155.00
1990 / 1495 / % 12.75
2000 / 6875 / % 359.87
Average change: % 175.87
How are the above percentage values computed? Let us look the first value: 155 = 100*(1326-520)/520
Basically, in 1980s, there was an increase of 1326-520=806 for this name. How big is this value
compared to the original 520? We find this by computing 100*806/520.
Note that for this part, we are not trying to produce a very polished output and we will not align
columns in the table for decades. We simply have a single space and slash (/) after each value.
Assume no zero values for any decade (so you will not get division by zero errors).
This is a relatively long program, but it is also repetitive. Let us try to employ the principle of
iterative programming to solve it.
• First, write a program to read one input and print it. What is a good name for your variable?
• Now, add another line to read the next input, print it. Also, compute the percentage change
from the previous one. Print this as well.
• Look at formatting. How do you print only two digits after comma? How do you print the
percent sign? Fix it.
• Now, worry about reading the name and print it. Also figure out how many stars to print in
the next line. How many are needed, given the length of the name?
• Now, add all the percentage values and divide by three to find the average. Display this value.
• You are not done yet! Don’t forget an important part. TESTING! Test with some simple
values: 1,2,4,8 and 1,2,6,24 (and decreasing values as well). What do you expect to see? Do
you match? Now, try some more challenging values.
When you have tested your code, please submit it as hw1_part3.py.
Finished and still want extra challenges to sharpen your programming skills?
Here are some ideas to try on your own (do not submit these): In part 1, try centering the box
dimensions in the center of box and modify your program to work for any number of characters for
the frame. In parts 2 and 3, try to write the program with and without formatted strings (even
floats can be formatted without formatted strings, though takes some effort). As a final challenge,
align the table for different decades by making sure each column is exactly 12 characters wide (do
not use a specialized function).