feat: P-1.32 completed and commented
This commit is contained in:
@@ -4,3 +4,94 @@ device. That is, each input to the calculator, be it a number, like 12.34 or 103
|
||||
done on a separate line. After each such input, you should output to the Python console what would be displayed on your
|
||||
calculator.
|
||||
"""
|
||||
# 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()
|
||||
Reference in New Issue
Block a user