OO Programming and Data Structures | CS 241

02 Prepare : Checkpoint A

Objective

Demonstrate use of function parameters and return values.

Overview

Write a Python 3 program that prompts the user for 3 postive numbers (or zero) and then adds them together. If the user enters a negative number, the program should reprompt them until they enter a postive number.

Divide your program into two functions:

Then, include the following code at the bottom of your program to tell it to start with the main function:


if __name__ == "__main__":
    main()

Sample Output


Enter a positive number: 3

Enter a positive number: 6

Enter a positive number: 42

The sum is: 51

Or another run:


Enter a positive number: -3
Invalid entry. The number must be positive.
Enter a positive number: -20
Invalid entry. The number must be positive.
Enter a positive number: 20

Enter a positive number: 40

Enter a positive number: -80
Invalid entry. The number must be positive.
Enter a positive number: 80

The sum is: 140

Automatic Grading Script (TestBed)

This assignment is pass/fail. In order to receive credit, it must pass the auto-grading test script (TestBed). You can run this script as many times as you like while you are working on the assignment. The same test script will be run by the instructor for credit, so you should not be surprised at the result.

In addition, because the purpose of this assignment is for you to practice using functions, you must include at least the 3 functions as described above to receive credit for this assignment.

To run the test script, log into the CS Department Linux system and run the testBed command, providing the class and test script to run as well as the Python program to use, for example:


testBed cs241/check02a check02a.py

Submission

Submit your program by logging into the CS Department Linux system, and running the submit command with your filename, for example:


submit check02a.py

You should then select the appropriate course (CS 241), instructor, and assignment (check02a) from the list.