From 65a790841e675c9c3707e74126982cd5ba73a054 Mon Sep 17 00:00:00 2001 From: baphim <269010393+baphim@users.noreply.github.com> Date: Thu, 1 Oct 2026 02:18:40 -0400 Subject: [PATCH] feat: P-1.32 completed and commented --- lab-3_4/P-1.32.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/lab-3_4/P-1.32.py b/lab-3_4/P-1.32.py index f9e3981..290a2a8 100644 --- a/lab-3_4/P-1.32.py +++ b/lab-3_4/P-1.32.py @@ -3,4 +3,95 @@ Write a Python program that can simulate a simple calculator, using the console device. That is, each input to the calculator, be it a number, like 12.34 or 1034, or an operator, like + or =, can be done on a separate line. After each such input, you should output to the Python console what would be displayed on your calculator. -""" \ No newline at end of file +""" +# define a function that parses the operators tuple into actual operators and calculates result of the expression +def calc(value1, operator, value2): + if operator == "+": + result = value1 + value2 + elif operator == "-": + result = value1 - value2 + elif operator == "*": + result = value1 * value2 + elif operator == "/": + # handles a ZeroDivisionError with an error message + try: + result = value1 / value2 + except ZeroDivisionError: + print("You can't divide by zero, enter a new divisor") + # exits the function cleanly so result never holds None, preventing errors + return None + return result +# define global tuple of potential operators +operators = ("+", "-", "*", "/" ) + +def calculate(): + value1 = None + operator = None + value2 = None + # prompts for user input, exits program if 'q' is entered + while True: + user_input = input("Enter a number, mathematic operator (+, -, *, /) or 'q' to quit: ") + if user_input == "q": + break + # if user inputs '=', prints message if nothing to calculate, prints 'value1' if stored + elif user_input == "=": + if value1 is None: + print("Nothing to calculate") + elif value2 is None: + print(value1) + # otherwise calls 'calc' function to solve the expression + else: + result = calc(value1, operator, value2) + # if ZeroDivisionError is caught in 'calc' resets 'value2' to None + if result is None: + value2 = None + # if expression is successfully solved, stores the answer in 'value1' for future use and prints it + else: + value1 = result + operator = None + value2 = None + print(value1) + # if user input is an operator and 'value2' is not set, stores the operator and prints current state of expression + elif user_input in operators: + if value2 is None: + operator = user_input + print(value1, operator) + # if 'value2' is set, calculates the value of stored expression + else: + result = calc(value1, operator, value2) + # if ZeroDivisionError is caught in 'calc' resets 'value2' to None + if result is None: + value2 = None + # otherwise sets answer of stored expression to 'value1' and 'operator' to the new operator entered, sets 'value2' to None + else: + value1 = result + value2 = None + operator = user_input + print(value1, operator) + else: + # if no operator is stored, tries input for valid float value + if operator is None: + try: + user_input = float(user_input) + # handle ValueError and print error message + except ValueError: + print("Please enter a valid number or operator") + continue + # Stores input as 'value1' and prints it + value1 = user_input + print(value1) + # if operator is stored, tries input for valid float value + else: + try: + user_input = float(user_input) + # handle ValueError and print error message + except ValueError: + print("Please enter a valid number or operator") + continue + # stores input as 'value2' and prints current state of expression + value2 = user_input + print(value1, operator, value2) + + + +calculate() \ No newline at end of file