 # Python cli calculator

Python cli calculator
0
#1

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(" - ADDITION")
print(" - SUBTRACTION")
print(" - MULTIPLICATON")
print(" - DIVITION")
print(" - 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(" - 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(" - 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(" - 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(" - 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(" - 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()

#2

Nice Work!

My suggestion is just to keep going. Maybe take this to the next step and build a simple GUI for it. The way you built the system makes it easily adoptable.

I figure it’s just a little more intuitive than trying to run:

``````\$ python cli_calculator.py
Enter Option: 3
Enter First Number: 2
Enter Second Number: 5
the answer is 10
``````

When I could just do:

``````\$ python
>>> 2 * 5
10
``````

A GUI would bring back the speed and convenience factor.

Other than that. Maybe extend functionality to fractional/decimal inputs since they are converted to integers currently (also, that means you might not need to convert the answer to a float for subtraction and addition operations right now).