M05 Assignments

Introduction

The lessons for M05 concentrate on modular programming, so every program should include multiple functions. See the General Requirements below for more information and tips about functions.

M05_Assignments

General Requirements

Every program should have a main() function defined first. The purpose of the main function is to control the program flow. It should do as little "work" as possible, and simply call other functions to get things done. Do not put all your code in the main() function!

Some functions will need to have parameters defined and some will return one or more values. Parameters do NOT need to be named the same as the values that are used to call the function. Example:

myname = "David"
myage = 65
showAge(myname, myage)


def showAge(username, userage):
print(f"The age if {username} is {userage}")

Parameters are accepted by position (not name), so the first value in the function call is assigned to the first parameter, the second value to the second parameter, etc. Each parameter assumes the data type of the value provided.

Return Values

Functions do not need to have a return value. A function that only displays output, for example, will not have a return value.

Often, most other functions in a program will return one or more values. A function returns a value to the calling statement using the return statement, followed by a value. Example:

sqft = getSquareFootage(width, length)


def getSquareFootage (wdth, hgt):
squarefeet = wdth * hgt
return squarefeet

If the function needs to return more than one value, a tuple is used. Tuples work similar to parameters. The values are returned to the calling statement by position.

Exercise #1 (Bowling Program Rewrite)

Rewrite the bowling scores program (M02 Assn1) using 3 functions. The main() function should primarily call functions 1 and 2.

In function 2, prompt the user for the number of scores to enter. Using a loop in main(), prompt the user for each score (in function 3). Validate the user input in function 2 to check for a blank entry. It is not necessary to check for a score between 1 and 300, inclusive. Finally, calculate the total and average of all scores and display as output. You may display the output in function 3 or use another function to do that.
Sample program design:

main()
score_count = getNumberofScores()
for index in score_count
score = getScore()
total += score
displayScores(score_count, total)

Testing: Your results should look similar to the image below.

m05 assn1 exercise #1 sample output
Figure 1: M05 Assn1 Exercise #1 Sample Output

Exercise #2 (Calculate Acres)

Write a program to calculate how many acres a plot of land covers, based on square footage. Prompt the user for the number of feet along the road (width) and the number of feet to the back of the property (depth). Research the formula for converting square feet into acres. Your program should have at least 3 functions: main(), one for the user prompt, and one to display the results.  You may use more than functions if that is convenient Use a tuple in function 2 to return the width and depth, in order to send them to function 3.

Testing: Your results should look similar to the image below.

m05 assn1 exercise #2 sample output
Figure 2: M05 Assn1 Exercise #2 Sample Output

Exercise #3 (Guess a Random Number)

Write a program that generates a random number in the range of 1 through 10, and then ask the user to guess the number. If the user’s guess is incorrect, the program should display “Too high, try again" or “Too low, try again”. If the user is correct the program should congratulate the user. Allow the user to continue playing (generate a new random number) or quit at any time. The program should include at least 3 functions: main(), getNumber() and getGuesses().

Tips:

  • You must import the random library at the top of your code
  • Use the same type of while() loop as in earlier programs to see if the user wants to keep playing
  • Use an if-elif-else set of statements to test whether the user's guess is too high, to low, or correct

Testing: Your results should look similar to the image below.

m05 assn2 exercise #3 sample output
Figure 3: M05 Assn2 Exercise #3 Sample Output

Exercise #4 (Calculate Sales Tax)

A retail company must file a monthly sales tax report listing the total sales for the month, and how much of state and county sales tax are due. The state sales tax rate is 5 percent and the county sales tax rate is 2.5 percent. Write a program that prompts the user to enter the total sales for the month, and then calculates the taxes due. The program should display the sales amount entered, the calculated state and county taxes, and the total tax due.

Specific requirements  include:

  • The program should be modular, using a main() function and at least 3 other functions
  • The main() function should do very little work, except calling the other functions
  • Prompting the user for the sales total should be done in a 2nd function that returns the sales total as a float
  • Declare the tax rates as global variables at the top of the code
  • Calculate the tax rates in a 3rd function (not in main() and not in the function above). This function should return a tuple with the state and county tax amounts
  • Display the output in a 4th function (it may calculate total tax due). The function parameters should include the total sales amount and the calculated state and country tax amounts

Testing: Your results should look similar to the image below.

m05 assn2 exercise #4 sample output
Figure 4: M05 Assn2 Exercise #4 Sample Output