Skip to content

Instantly share code, notes, and snippets.

@Jithender5913
Last active December 20, 2021 03:37
Show Gist options
  • Select an option

  • Save Jithender5913/a0e2182600893b5d7cbc74c0d546bc69 to your computer and use it in GitHub Desktop.

Select an option

Save Jithender5913/a0e2182600893b5d7cbc74c0d546bc69 to your computer and use it in GitHub Desktop.
Calculator project using python
# calculator
# ADD
def add(n1, n2):
return n1 + n2
# subtracting
def subtract(n1, n2):
return n1 - n2
# Multiplication
def multiply(n1, n2):
return n1 * n2
# division
def divide(n1, n2):
return n1 / n2
operators = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
}
# create functions for operators function = operator["*"] then we can call this as function(2,3) output will be 6.
def calculator():
num1 = float(input("what is the first number?: "))
for symbol in operators:
print(symbol)
calc_repeat = True
while calc_repeat:
num2 = float(input("what is the second number?: "))
operational_symbol = input("Pick an operation from above: ")
calculation_function = operators[operational_symbol]
first_answer = calculation_function(num1, num2)
print(f"{num1} {operational_symbol} {num2} = {first_answer}")
user_choice = input("do you want to calculate again? type yes or no ")
if user_choice == "yes":
calc_repeat = True
else:
calc_repeat = False
print("bye")
calculator()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment