Winter 2023
By this time in the quarter you’ve seen functions in lecture, in lab, and from here on they will be required in the coding portions of the homework assignments, including A4.
In this lab, you’ll write a program that calculates the total square footage in a home. Imagine you are working for a construction company that builds homes with only square or rectangular rooms. The houses vary in size, with as little as 1 room and up to 100s. Your task is to write a program that will prompt the user to input the number of rooms and their dimensions, and which then calculates the total square footage of the home. Two invocations of such a program are shown below.
In the first example, a user specifies a house with 3 rooms – a square room and two rectangle rooms. The second shows a calculation for a house with 4 rooms – three square rooms and a rectangular room.
You’ll create such a program from scratch, following good coding style. In particular, if you find yourself repeating similar code, it’s often a good idea to create a function that performs that repeated task then call it as needed. In the program invocations above, notice the following:
To calculate the size of a square room, you need only the dimension of one of the walls
To calculate the size of a rectangular room, you need both dimensions, the width and length
There are repeated print statements that are very similar, but not identical. However, they all begin with “What is”, and end with a question mark.
From these observations, your supervisor has decided that your program can have ONLY 3 functions:
room_square_feet
, which takes a single argument to
designate whether the room is a square or rectangular room. If the room
is a square room, the function room_square_feet
invokes the
prompt_user
function once to retrieve the single dimension
needed to calculate the size of the room. If the room is a rectangular
room, the function room_square_feet
invokes the
prompt_user
function twice to retrieve the two dimensions
needed to calculate the room’s size. The function
room_square_feet
returns the square footage of a
room.
main
, which prompts the user for the count of rooms
and calls room_square_feet
for each room, using the
accumulator pattern (refer to Section 6.4 of the textbook) to keep a
running total of the square footage of the house.
prompt_user
, which receives two arguments and
returns one value.
str
, is the text of the
question that the user is prompted. In the example output above,
prompt_user is called with the first argument set to
"length of the room"
, "width of the room"
and
"shape of room 1, square or rectangle"
on different
occasions.Create a lab6
folder and in it create a Python file
called squarefootage.py
.
The steps below illustrate an example of the program’s behavior when
prompting for the information and calculating the square footage of a
single room. Recall that your main program will do this once for each
room in the house. Notice that main
uses
prompt_user
twice, then calls
room_square_feet
, which itself calls
prompt_user
once or twice depending on the shape of the
room.
prompt_user
is invoked with first and second
arguments “shape of room 1” and “string” both of type string.
prompt_user
prompts the user with the question “What
is the shape of room 1?”
The user responds to the prompt and types “square”
prompt_user
returns the string “square”, which is
then passed to the function room_square_feet
The function room_square_feet
calls
prompt_user
with two arguments, “side length of square
room” and “integer”
The function prompt_user
prompts the user with the
question “What is the side length of square room”
The user responds to the prompt and types “12”
The function prompt_user
returns the integer 12,
which is saved in a variable in the function
room_square_feet
The function room_square_feet
calculates the size of
the room (\(12 * 12\)) and returns the
integer 144.
All three functions are fairly simple, taken by themselves. You should be able to write each one in less than about 10 lines of code.
Start by writing a header and specification for the
prompt_user
function based on the description above. Then
implement the function and test it using the interactive shell. Test it
for both integer and string output types.
Write a header and specification for
room_square_feet
. Implement it, using your already-tested
prompt_user
function, and test its functionality in the
interactive shell.
Write the main
function, noting that it takes no
arguments and has no return value; when the total square footage is
calculated, print the total.
Finally, at the bottom of your program, call your
main
function in a main guard so that running the program
calls the main function:
if __name__ == "__main__":
main()
As usual, you are not required to check for and
handle input from badly behaved users: you may assume the user enters
numbers when they’re supposed to, and that they enter either
square
or rectangle
for the shape of each
room. Notice, however, that because we’ve chosen to break
prompt_user
out as it’s own function, error checking is
easier to add: you only need to do it in one place. This is a smart
practice!
We test your programs with automated scripts, so the prompts must be made in exactly the order shown in the sample outputs to receive full credit. The text of the prompts does not need to match exactly, but the order must match.
Submit squarefootage.py
via Canvas.
This lab is graded out of 30 points.
squarefootage.py
and has a
comment specifying author, date, and purpose at the top.main
, room_square_feet
, and
prompt_user
have headers and docstrings consistent with the
description in the lab handoutprompt_user
prompts the user with the
provided question and returns the correct typeroom_square_feet
uses
prompt_user
to prompt for the dimensions of a roommain
uses prompt_user
to specify
how many roomsmain
calls prompt_user
to prompt
for shape of the roommain
calls room_square_feet
for each room
and the return value is added to an accumulator variablemain
function prints correct final square
footage