I made this basic Python calculator. It just does addition, subtraction, division, and multlipication.
I feel like the code is a mess, but it does what I want it to, and I feel like it does it effectively.
import operator
def calculate (first_number, operation_type, second_number):
if operation_type == 'a':
answer = first_number + second_number
answer_text = f"{first_number} + {second_number} = {answer}"
if operation_type == 's':
answer = first_number - second_number
answer_text = f"{first_number} - {second_number} = {answer}"
if operation_type == 'm':
answer = first_number * second_number
answer_text = f"{first_number} x {second_number} = {answer}"
if operation_type == 'd':
answer = first_number / second_number
answer_text = f"{first_number} \xF7 {second_number} = {answer}"
return answer_text
while True:
while True:
try:
first_number = int(input("Enter first number: "))
break
except ValueError:
print ("Enter a number!\n")
pass
while True:
operation_type = str(input("\nA - add\nS - subtract\nM - multiply\nD - divide\nEnter operation type: "))
operation_type = operation_type.lower()
if operation_type in {'a', 'm', 'd', 's'}:
break
else:
print ("Invalid operation type!\n")
pass
while True:
try:
second_number = int(input("\nEnter second number: "))
break
except ValueError:
print ("Enter a number!\n")
pass
print (calculate (first_number, operation_type, second_number))
run_again = str(input("Run again? Type 'y' to continue, or q to quit: "))
if run_again.lower() == 'y':
pass
if run_again.lower() == 'q':
break
elif run_again.lower() != 'y' and run_again.lower() != 'q':
print ("Invalid entry")
break