Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions Calculator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ def exponent(ne1, ne2):
"""This function will return the power of two numbers"""
return ne1**ne2


def nth_root(nr1, n):
"""Getting the nth root of nr1"""
return nr1**(1/n)


def subtract(ns1, ns2):
""" This function will return the difference of two numbers """
return ns1 - ns2
Expand All @@ -26,40 +28,73 @@ def divide(nd1, nd2):
return nd1 / nd2


def modulus(nm1, nm2):
""" This function will return the modulus of two numbers """
return nm1 % nm2


operations = {
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
"^": exponent,
"√": nth_root
"√": nth_root,
"%": modulus
}


def calculator():
""" This function contains the code that will work as you wish to proceed \
an operation """
num1 = float(input("What's the first number?(to pick √, hold alt + 251 (on numpad)): "))
for symbol in operations:
print(symbol)
should_continue = True

while should_continue:
operation_symbol = input("Pick an operation: ")
num2 = float(input("What's the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
if operation_symbol == "√":
print(f"{num2} {operation_symbol} {num1} = {answer}")
else:
print(f"{num1} {operation_symbol} {num2} = {answer}")
should_continue = False
# Input Validation so only numbers can be entered
while not should_continue:
try:
num1 = float(input("What's the first number?(to pick √, hold alt + 251 (on numpad)): "))
should_continue = True
except Exception as err:
print("Please enter a number.")
should_continue = False
print("")

if input(f"Type 'y' to continue calculating with {answer},\
or type 'n' to start a new calculation: ") == "y":
num1 = answer
should_continue2 = False
while not should_continue2:
operation_symbol = input("Pick an operation: +, -, *, /, ^, √, %: ")
if operation_symbol in ("+", "-", "*", "/", "^", "√", "%"):
should_continue2 = True
else:
should_continue = False
calculator()
print("Please enter a valid operation")
should_continue2 = False
print("")

should_continue3 = False
# Input Validation so only numbers can be entered
while not should_continue3:
try:
num2 = float(input("What's the next number?: "))
should_continue3 = True
except Exception as err:
print("Please enter a number.")
should_continue3 = False
print("")

calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
if operation_symbol == "√":
print(f"{num2} {operation_symbol} {num1} = {answer}")
else:
print(f"{num1} {operation_symbol} {num2} = {answer}")

if input(f"Type 'y' to continue calculating with {answer},\
or type 'n' to start a new calculation: ") == "y":
num1 = answer
print("")
else:
should_continue = False
print("")
calculator()


calculator()