CSCI 141 - Lab 6

Scott Wehrwein

Winter 2023

Introduction

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:

From these observations, your supervisor has decided that your program can have ONLY 3 functions:

  1. 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.

  2. 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.

  3. prompt_user, which receives two arguments and returns one value.

Setup

Create a lab6 folder and in it create a Python file called squarefootage.py.

Example Program Flow

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.

  1. prompt_user is invoked with first and second arguments “shape of room 1” and “string” both of type string.

  2. prompt_user prompts the user with the question “What is the shape of room 1?”

  3. The user responds to the prompt and types “square”

  4. prompt_user returns the string “square”, which is then passed to the function room_square_feet

  5. The function room_square_feet calls prompt_user with two arguments, “side length of square room” and “integer”

  6. The function prompt_user prompts the user with the question “What is the side length of square room”

  7. The user responds to the prompt and types “12”

  8. The function prompt_user returns the integer 12, which is saved in a variable in the function room_square_feet

  9. The function room_square_feet calculates the size of the room (\(12 * 12\)) and returns the integer 144.

Game Plan and Guidelines

Submission

Submit squarefootage.py via Canvas.

Rubric

This lab is graded out of 30 points.