Skip to content

Instantly share code, notes, and snippets.

@JAKAMI99
Forked from complxalgorithm/calculator.py
Last active August 21, 2021 11:26
Show Gist options
  • Save JAKAMI99/cfdd4d61b588c15a071883e1c5ca4fe6 to your computer and use it in GitHub Desktop.
Save JAKAMI99/cfdd4d61b588c15a071883e1c5ca4fe6 to your computer and use it in GitHub Desktop.
Simple Python calculator program.
# Program make a simple calculator that can add, subtract, multiply and divide using functions
#import
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user with coloring the first digit
print('\033[2;31;43m A \033[0;0m', end= "")
print("dd")
print('\033[2;31;43m S \033[0;0m', end= "")
print("ubtract")
print('\033[2;31;43m M \033[0;0m', end= "")
print("ultiply")
print('\033[2;31;43m D \033[0;0m', end= "")
print("ivide")
choice = None
while choice not in ("A", "S", "M", "D"):
choice = input("Enter your choice: ")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == 'A':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == 'S':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == 'M':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == 'D':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
@JAKAMI99
Copy link
Author

Since I wanted to learn some python, I edited the code to make sure the user inputs a valid input and I colored the choices :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment