Week 3
Reinforcing selection, iteration, and function calling.
Instructions
For all of the following, write both the function and some code that calls and tests the function.
Complete the following without using ANY outside aid. If necessary, refer only to the python help() command or the official documentation at https://python.org.
Use PyCharm to complete this task. I know that PyCharm has a built-in AI Assistant. I strongly recommend that you disable it. You are going to be quizzed on these skills in class, and you will not be allowed to use any outside assistance.
- Write a function named
multiply()with two parameters,aandb, that returns the result.- Verify that both
aandbare integers. - Return nothing if either
aorbis not an integer.
- Verify that both
- Write a function named
divide()with two parameters,aandb, that returns the result ofa/b.- Verify that both
aandbare either integers. - Return nothing if either
aorbis not a number. - Return nothing if
bequals0.
- Verify that both
- Write a
calculatorfunction- The function must contain an “infinite” while loop that does the following until the user chooses to ’exit'.
- Prompt the user to make a choice of either
multiply,divide, orexit. - Do not allow or handle an invalid choice.
- If the user picks
exit, the program must end. - Prompt the user to enter two values,
aandb. - Based on their choice, call either your
multiply()ordivide()function. - If either
multiply()ordivide()returns nothing, print an error message. - Otherwise, print the result in the format, e.g,
4 * 5 = 20or2.4 / 1.2 = 2.0. - Round the
divideresult to the tenths place using the built-inround()function when printing.
Submission
Submit your .py file to Canvas for a check. You are not graded on completeness or correctness – this is for learning and feedback.
Key Skills
- Function definition: parameters and returns.
- Logical selection using
if. - Type checking using
isinstance(). - Functions calling functions.