Microsoft Word - PyLabS21-Lab4_Calculator3.docx Lab activity #4: Implement decomposition with functions Program: Calculator, version 3 Overview  Objective: A user inputs two numbers & chooses any one...

Can I ask for your help please?


Microsoft Word - PyLabS21-Lab4_Calculator3.docx Lab activity #4: Implement decomposition with functions Program: Calculator, version 3 Overview  Objective: A user inputs two numbers & chooses any one of the following four arithmetic operations: addition, subtraction, multiplication, division. The result of the calculation is displayed. The user can repeat calculations until s/he decides to quit. The program is constructed for maintainability.  Python constructs used: flow control, type casting, arithmetic operator, functions. Detailed program description The purpose of this program is to setup a calculator that is maintainable.  The program is named ~u8245/pyscr.d/submit/calculator3.py.  Functions are defined before the main code.  Functions are called in the main program code. Main program  Display menu.  Prompt for the operator.  Prompt for two numbers.  Based on operator selection perform calculation. o Division by zero: If the divisor is zero re-prompt for a valid divisor.  Display the result: create only ONE display statement after your decision logic.  The user can repeat calculations until s/he invokes a quit option. Guideline: A simple main program, excluding function definitions, comments and empty lines, has around 25 LOC (lines of code). Function definitions Decompose the program into functions: one function per operation. Keep I/O in main program.  Create a function to display the menu. However, do NOT request user input from within that function. Name it displ_menu. o Function body: display menu options (one option per line, both in code and on screen display) o Function parameters: none o Function return: none  Create a function to prompt the user for a number. Name it: get_float(). o Function body: prompt for number o Function parameters: prompt (the sentence used to prompt the user for input) o Function return: the number entered by the user & converted to float  Create a function for each calculation that is performed by the calculator. Name them: add, subtract, multiply, divide. o Function body: perform arithmetic o Function parameters: two operands o Function return: the result of the calculation  Create a function that returns True if the number is ‘0’. Name it: iszero. o Function body: test whether a number is zero o Function parameters: a number o Function return: True or False Guideline: All function definitions combined, excluding comments and empty lines, have around 20 LOC (lines of code). Sample run Menu options: +: to add -: to subtract *: to multiply /: to divide Enter operator (q to quit): + Enter first number: 12 Enter second number: 8 -> Result of calculation: 12.0 + 8.0 = 20.0 # Optionally display menu again Enter operator (q to quit): / Enter first number: 12.0 Enter second number: 0.0 Division by zero cannot be performed. Enter a non-zero divisor: 2.0 -> Result of calculation: 8.0 / 2.0 = 4.0 # Optionally display menu again Enter operator (q to quit): q Closing calculator. Test plan Test ID Test purpose Test data Expected result Actual result 1 Perform an addition with two numbers. 2 + 3 2 Perform a subtraction with two numbers. 2 – 3 3 Perform a multiplication with two numbers. 3 * 3 4 Perform a division with two numbers. 12 / 8 5 Perform a division with the divisor being zero. 12 / 0 displays an error message; re-prompts for divisor 6 Perform multiple operations before quitting Multiple calculations, then “q” Quits after multiple runs 7 Quit without performing a calculation “q” upon starting program Quits right away
Sep 29, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here