Lab 7

Introduction:

In this lab, you will practice writing functions and a module.

Functions

You will write three functions, as described below. The Module section of this lab talks about how these functions will be used and where each function will be saved. Be sure to also properly document your functions.

input_int Function

The input function takes a string and returns whatever the user entered (as a string). Write a function called input_int that takes a string (the prompt to display to the user) and return the user's response as an int. If the user does not enter an int, display an error message and ask again. Continue asking until the user enters an int. Use exception handling to check whether it is an int. If the programmer using your function passes into the function "Please enter an integer: ", the user of that program should see:

Please enter an integer: cat
You must enter an integer.
Please enter an integer: dog
You must enter an integer.
Please enter an integer: 13

input_float_positive Function

The input function takes a string and returns whatever the user entered (as a string). Write a function called input_float_positive that takes a string (the prompt to display to the user) and return the user's response as a float. If the user does not enter a positive float, display an error message and ask again. Continue asking until the user enters a valid float. Use the input_float function from Lab 6 to get floats from the user.

If the programmer using your function passes into the function "Please enter a positive number: ", the user of that program should see:

Please enter a positive number: cat
You must enter a positive number.
Please enter a positive number: -33.4
You must enter a positive number.
Please enter a positive number: 5003.2

Calorie Calculator

Write a function that takes two values:

The function then returns the calories, according to the following formula and table:

calories = grams * energy_density

with energy_density coming from:

Food Component (list of possible names) Energy Density
Alcohol, ethanol 7
Carbohydrate, sugar, starch 4
Fat, lipid 9
Protein, meat 4

For example, if you have 3.8 grams of meat, then that is 3.8 * 4 = 15.2 calories.

Your function should validate its arguments. If the arguments are invalid, raise the appropriate exception.

Module

Create a module named input_valid. In this module, have the functions:

To demonstrate using the module, in a separate source code file, use this module to get the number of grams of a food component. Also ask for a food component (see Calorie Calculator above for valid food components). Pass in the user's inputs into the calorie calculator function described above and display the calories returned. If the user does not enter a valid food component, ask again until they do.

For example:

What food component are you interested in? meat
How many grams? 3.8
That is 5.2 calories.

Final Notes:

Remember to properly document your functions.

Submission and Grading:

Complete the Assignment Information Sheet.

Submit your final program and assignment information sheet (zipped into one file) to CourseWeb in the Lab 7 assignment.

The grading rubric can be found here: Rubric (doc).

The assignment is due Monday, July 13 by 11:30 am. As with all programming assignments, you have unlimited uploads (before the deadline), so you may upload the assignment before the deadline. If you later decide to upload another, you may do so without penalty (as long as it's before the assignment deadline). The last submission uploaded before the deadline will be the one graded. If you would like ungraded feedback on a programming assignment, you may send an email to your TA or the instructor and ask for feedback; please send your code as well.

For more advice on submitting your assignment, see the Programming Assignments section of the Tips for Success page.