Hi everyone!
I’m new here and i just started learning python this month, Im trying to create a command line calculator using python. any suggestions on how i can improve my work, my code is located below. thanks a lot!
import os
#main menu function
def main_menu():
os.system(“cls”)
print(“CALCULATOR PROGRAM”)
print("\nOPTIONS")
print("[1] - ADDITION")
print("[2] - SUBTRACTION")
print("[3] - MULTIPLICATON")
print("[4] - DIVITION")
print("[0] - EXIT PROGRAM")
#asks for the user input
option = input("\nEnter Option: ")
#asks the user to enter the correct option
while option not in ["1", "2", "3", "4", "0"]:
print("Invalid Option!")
option = input("\nEnter Option: ")
#calls the function that matchs the option
if(option == "1"):
add()
elif(option == "2"):
subtract()
elif(option == "3"):
multiply()
elif(option == "4"):
divide()
elif(option == "0"):
exit_program()
#add function
def add():
os.system(“cls”)
print("[1] - ADDITION")
number1 = int(input("\nEnter First Number: "))
number2 = int(input("Enter Second Number: "))
ans_1 = number1 + number2
print("the answer is ",float(ans_1))
os.system(“pause”)
#going back to the main program
main_menu()
#subtract function
def subtract():
os.system(“cls”)
print("[2] - SUBTRACTION")
number1 = int(input("\nEnter First Number: "))
number2 = int(input("Enter Second Number: "))
ans_1 = number1 - number2
print("the answer is ",float(ans_1))
os.system(“pause”)
#going back to the main program
main_menu()
#multiply function
def multiply():
os.system(“cls”)
print("[3] - MULTIPLICATION")
number1 = int(input("\nEnter First Number: "))
number2 = int(input("Enter Second Number: "))
ans_1 = number1 * number2
print("the answer is ",float(ans_1))
os.system(“pause”)
#going back to the main program
main_menu()
#divide function
def divide():
os.system(“cls”)
print("[4] - DIVISION")
number1 = int(input("\nEnter First Number: "))
number2 = int(input("Enter Second Number: "))
#holds the operation when the user performs division by zero
if(number2 == 0):
print("Error! Division by Zero!")
divide()
else:
ans_1 = number1 / number2
print("the answer is ",float(ans_1))
os.system("pause")
#going back to the main program
main_menu()
#exit function
def exit_program():
os.system(“cls”)
#asking the user to exit/stay in the application
print("[0] - EXIT PROGRAM")
ext = input("\nExit Calculator?(y/n) ")
#same loop structure used previously
while ext not in ["y", "n"]:
print("Invalid Input!")
ext = input("\nExit Calculator?(Y/N) ")
#after going through the loop
if(ext == "y"):
print("Thnak you....")
exit()
elif(ext == "n"):
#going back to the main program
main_menu()
#calling the main ptogram
main_menu()