Python Programming Assignment
In all programs:
- YOU MUST TO WRITE INFORMATIVE COMMENTS!
- You MUST use Top-Down Design
- Each subtask MUST be
a
separate function
- You MUST have function MAIN in your program.
- In this lab you WILL NOT SHOW me the output and the source file. It will be YOUR responsibility to TEST
your program
- Submission: print source file and lab report. You can write lab report in the same file AFTER the
program
but make sure you start each line with # to indicate comments. You can also write lab repot on the paper.
Lab Report for Each Program must include TESTING PART ONLY:
For each test input, document the output your program produced. If the output is NOT correct,
document the list of action you performed to correct logical errors.
Problem 1
:
Write the following functions:
- Function sumInput that has one parameter - the number of input integers. The function inputs integers and finds
their sum.
USE
LOOP FOR in this function.
- Function productInput that has one parameter - the number of input integers. The function inputs integers and finds
their
product.
USE
LOOP WHILE in this function.
- Function average that has NO parameters. The function inputs the series of positive integers. The first zero or negative
terminates the input. The function finds the average of the input numbers. In this function you can only use LOOP WHILE.
- Function menu that has one parameter - choice. The function performs the following: if the choice is 1, user is asked to
enter
the number of inputs and the function sumInput is called. If the choice is 2, user is asked to enter
the number of inputs and the function productInput is called. If the choice is 3, function average is called. For all other
choices, function outputs error message.
- Write main function that reads one integer - user's choice and call function menu.
Problem 2
:
Write the following functions:
- Function isPrime that has one parameter - integer number. The function prints YES if the parameter is prime and NO
otherwise.
Prime number is a number that has exactly 2 divisors - 1 and the number itself. By definition, 1 is NOT prime number.
- Function isPerfect that has one parameter - integer number. The function prints YES if the parameter is perfect number
and NO
otherwise. The number called perfect if the sum of the proper divisors equals to the number itself. Proper divisor is the
divisor that is less than the number. Example: 6 is perfect. Proper divisors of 6 are 1, 2, and 3 and their sum is 6.
Other perfect numbers, for example, are 28, 496, 8128. See HERE for more information.
- unction menu that has one parameter - choice. The function performs the following: if the choice is 1, user is asked to
enter
the number and the function isPrime is called. If the choice is 2, user is asked to enter
the number and the function isPerfect is called.
For all other
choices, function outputs error message.
- Write main function that reads one integer - user's choice and call function menu.