Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Calculator Objective: practicing with implementing loops, elif statements, functions and string methods Description In this assignment you will write a Python program (named calculator.py)

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

A Calculator Objective: practicing with implementing loops, elif statements, functions and string methods Description In this assignment you will write a Python program (named calculator.py) that emulates a simple calculator that can add, subtract, multiply or divide two numbers. Your program will ask the user to choose an operation (addition, subtraction, multiplication or division) and two numbers and then outputs the result. Here are the snippets of the program output. Note: to get the full credit please make sure that your program output matches this format exactly. Welcome to Calculator Program! Please choose one of the following operations: Addition - A Subtraction - S Multiplication - M Division - D > The program acknowledges the user's choice. For example, if the user enters A the program should output: You chose addition. In other legitimate cases, it provides the same type of output with the corresponding choice. Otherwise, it warns about wrong input and reprints the previous prompt: You did not choose correctly. Please choose one of the following operations: Addition - A Subtraction - S Multiplication - M Division - D If the user provided the correct input the program asks for the first number: Please enter the first number: If the user does not enter a number, the program should warn about the wrong input and repeats the previous prompt. You did not choose a number. Please enter the first number: If the user chose 2 then the program outputs: The first number is 2. Please enter the second number: If the user does not enter a number, the program should warn about the wrong input and repeats the previous prompt. You did not choose a number. Please enter the second number: If the user chose 5 then the program outputs: The second number is 5. After that the program performs calculations. If the user previously chose addition then the program outputs: 2 + 5 = 7 If the user chose multiplication then the program outputs: 2 x 5 = 10 If the user chose subtraction then the program outputs: 2 - 5 = -3 If the user chose division then the program outputs: 2 / 5 = 0.4 For all division operations, if the second number is 0 then the program outputs: The division by zero is prohibited! After that the program asks the user if he or she wants to quit or continue: Do you want to continue? [Y/N]: If the user enters "Y" of "y" the programs restarts the calculator and prints the following messages again: Please choose one of the following operations: Addition - A Subtraction - S Multiplication - M Division - D Otherwise, the program prints the following message and terminates: Goodbye! Programming Approaches You can use the template file calculator.py posted in Files/Scripts/PA2. Remember your program should start with the header (a comment block): # assignment: programming assignment 3 # author: (type your full name here) # date: (type the date you finished working on the program) # file: calculator.py is a program that (type the description of the program) # input: (type the input description) # output: (type the output description) In this assignment you should create at least five functions that perform arithmetic calculations and format the result as a string (the format function should return a number as a string). Their definition headings should be written exactly as the following function definition headings: 2): def format (num, precision (should return a string) def add (numi, num2): (should print a math equation to the stdout) def subtract (numi, num2): (should print a math equation to the stdout) def multiply (numl, num2): (should print a math equation to the stdout) def divide (num1, num2): (should print a math equation to the stdout) In general, every program should be error-proof: it should not end prematurely (crash) and produce wrong output. It can be a challenge because programs take various input data and there is always a chance that the input data will be in a wrong format. For example, let's analyze this statement: numl - int (input ("Please enter the first number: ")) What will happen when the user enters a float number or a string? The program will crash!!! In this assignment you will use a safe approach to avoid this type of crash by using a function isfloat(): In general, every program should be error-proof: it should not end prematurely (crash) and produce wrong output. It can be a challenge because programs take various input data and there is always a chance that the input data will be in a wrong format. For example, let's analyze this statement: numl - int (input ("Please enter the first number: ")) What will happen when the user enters a float number or a string? The program will crash!!! In this assignment you will use a safe approach to avoid this type of crash by using a function isfloat(): def isfloat (token) : if token == '': return false dot = False minus = False for char in token : if char.isdigit(): # allow many digits in a string continue elif char == "." : # allow only one dot in a string if not dot : dot = True else: return false elif char == "-" and token[O] == "-": # allow one minus in front if not minus : minus = True else: return false else: # do not allow any other characters in a string return false return True You need to check if a string entered by the user is a number, a float or an integer that can be converted into a float. So, you need to use the function isfloat() that analyzes a string and returns Boolean True if the string has a float or an integer format, otherwise it returns False. There are several string methods in Python; however, they are only useful to check if a string has an integer format (not a float): isdecimal()- returns True if all characters in a string are decimal [0 - 9] isdigit() returns True if all characters in a string are digits [0 - 9] including superscripts and subscripts isnumerico- returns True if all characters in a string are numeric including rational numbers Formatting Numbers: Formatting floating numbers for output can be done using the string method format() or the round() function or other methods. In this assignment you will use your own function format() for formatting numbers. This function format() can be based on using the built-in function round() or string method format() or anything else. Here is the descriptions how the string method format() and built-in function round() work. String Format Method: You can specify the precision of numbers using the following formulas within the format method: '{:04.2f}'.format(3.141592653589793) OUTPUT: 3.14 '{:06.2f}'.format(3.141592653589793) OUTPUT: 003.14 Function Round: the function round (number [, precision]) has two parameters, the parameter number is an integer or a float, and the parameter precision is the number of decimal places, the default value of the precision parameter is O, thus the function round of a float number returns an integer. For example, round (9) returns 9 round (9.5) returns 10 round (9.3) returns 9 round (1.20000, 2) returns 1.2 round (1.2, 2) returns 1.2 round (1.23, 2) returns 1.23 round (1.234, 2) returns 1.23 round (1.236, 2) returns 1.24 Format Function Specifications: All numbers should be formatted for output according to these two rules: 1. If a number has more than two decimal places it should be rounded to two decimal places. 2. No insignificant zeroes should be shown. For example: 1.234 becomes 1.23 (rounded to two decimal places) 1.200 becomes 1.2 (two insignificant zeroes are deleted) 1.000 becomes 1 (three insignificant zeroes and the decimal point are deleted) So, if the user chose division and entered 2.0 and 5.00, then the output is: The first number is 2. The second number is 5. 2 / 5 = 0.4 Use the round function ONLY for formatting! Do not round numbers otherwise, the number's internal representation should not be changed or rounded

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago