Contributors: Chris Chen, David Chen, Alan Chen
The program takes a user input for a calculation involving two numbers. It then identifies the operator entered, performs the calculation, and returns the result for the user to see.
Explanation of Code
Lines 31-36: Initialize variables:
"user_input" (store the input of the user)
"user_input_split" (a list with the split sections by space of the user_input string),
"operator" (to store the identified arithmetic operator that the user inputs)
"num1" and "num2" (to store the 2 identified numbers for calculation)
"result" (store the calculated result)
Lines 39-45: A function named "add" with two parameters: num_1 and num_2 is defined. The function will then return the sum of num_1 and 2.
Lines 47-53: A function named "subtract" with two parameters: num_1 and num_2 is defined. The function will then return the difference of num_1 and 2.
Lines 55-61: A function named "multiply" with two parameters: num_1 and num_2 is defined. The function will then return the product of num_1 and 2.
Lines 63-73: A function named "divide" with two parameters: "num_1" and "num_2" is defined. An if statement is defined on line 70 which checks if "num_2" is 0 (the denominator). If so, the result should be "UNDEFINED" and an error message will be displayed beside the result, telling the user it is a division by 0 error. Otherwise, the function will return the quotient of "num_1" and "num_2".
Line 75: A while loop is created to continuously allow the user to enter calculations until they enter "end" which will break the while loop.
Line 77: Using the input() function, the user is prompted to enter what needs to be calculated and is stored in the variable "user_input".
Lines 80-81: Using an if statement, check if the user input from the "user_input" variable is "end" (input converted to lowercase for comparison). If it is, break out of the while loop and exit the program.
Line 84: The user input from the "user_input" variable is split by spaces and put into a list using the ".split()" method. Then it is stored in the variable "user_input_split".
Line 87: After the user input is split, we know that the arithmetic operator will be at index 1 of the "user_input_split" list. The list will be laid out like this: ["num1", "operator", "num2"]. The fetched operator will then be stored in the "operator" variable.
Lines 90-91: The same process used to fetch the operator is done to fetch the two numbers, which are located at index 0 and index 2, respectively. They are then converted to floats using the "float" function and stored in their respective variables: "num1" and "num2".
Lines 94-116: If and elif statements are used to check which operator the user has entered: "x" or "*" (multiply), "+" (addition), "-" (subtraction), and "/" (division). Their respective functions are called with the parameters filled in with the variables "num1" and "num2", and then the result is stored in the "result" variable. Finally, the result is printed out for the user to see.
Lines 118-119: An else statement is used to handle if an invalid operator is inputted or if the operator couldn't be detected. An error message is then printed for the user to see.
Line 122: After the loop breaks because "end" was entered, a message is printed out to inform the user that the program has stopped.
Sample Inputs and Output:
Give the program a try here: https://replit.com/@ChrisChen42/Simple-Calculator-Program?v=1