# 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

os.system(“cls”)
print(“CALCULATOR PROGRAM”)
print("\nOPTIONS")
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"):
elif(option == "2"):
subtract()
elif(option == "3"):
multiply()
elif(option == "4"):
divide()
elif(option == "0"):
exit_program()
``````

os.system(“cls”)
number1 = int(input("\nEnter First Number: "))
number2 = int(input("Enter Second Number: "))
ans_1 = number1 + number2
os.system(“pause”)

``````#going back to the main program
``````

#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
os.system(“pause”)

``````#going back to the main program
``````

#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
os.system(“pause”)

``````#going back to the main program
``````

#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
os.system("pause")

#going back to the main program
``````

#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
``````

#calling the main ptogram

#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
``````\$ python