#TODO: Write the functions for arithmatic operations here
#These functions should cover Task 2
def add(a,b):
return a+b
def substract(a, b):
return a - b
def multiply(a,b):
return a*b
def divide(a, b):
try:
return a/b
except Exception as d:
print(d)
def power(a,b):
return a ** b
def remainder(a,b):
return a%b
#-------------------------------------
#TODO: Write the select_op(choice) function here
#This function sould cover Task 1 (Section 2) and Task 3
def select_op(choice):
if choice == '#':
return -1
elif choice == '$':
return 0
elif choice in ('+','-','*','/','^','%','#','$'):
while (True):
first_num = str(input('Enter first number: '))
print(first_num)
if first_num.endswith ('$'):
return 0
if first_num.endswith ('#'):
return -1
try:
num1 = float(first_num)
break
except:
print("Not a valid number,please enter again")
continue
while (True):
sec_num = str(input('Enter second number: '))
print(sec_num)
if sec_num.endswith ('$'):
return 0
if sec_num.endswith ('#'):
return -1
try:
num2 = float(sec_num)
break
except:
print("Not a valid number,please enter again")
continue
#2.0 + 4.0 = 6.0
if choice == '+':
print(num1, '+', num2, '=', add(num1,num2))
elif choice == '-':
print(num1, '-', num2, '=', substract(num1,num2))
elif choice == '*':
print(num1, '*', num2, '=', multiply(num1,num2))
elif choice == '^':
print(num1, '^', num2, '=', power(num1,num2))
elif choice == '/':
print(num1, '/', num2, '=', divide(num1,num2))
elif choice == '%':
print(num1, '%', num2, '=', remainder(num1,num2))
else:
print("Something Went Wrong")
else:
print("Unrecognized operation")
#End the select_op(choice) function here
#-------------------------------------
#This is the main loop. It covers Task 1 (Section 1)
#YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE
while True:
print("Select operation.")
print("1.Add : + ")
print("2.Subtract : - ")
print("3.Multiply : * ")
print("4.Divide : / ")
print("5.Power : ^ ")
print("6.Remainder: % ")
print("7.Terminate: # ")
print("8.Reset : $ ")
# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
print(choice)
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
exit()
**Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ Enter choice(+,-,,/,^,%,#,$): + + Enter first number: 4 4 Enter second number: 5 5 4.0 + 5.0 = 9.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ Enter choice(+,-,,/,^,%,#,$): $** **$ Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ Enter choice(+,-,,/,^,%,#,$): / / Enter first number: 5 5 Enter second number: 0 0 float division by zero 5.0 / 0.0 = None Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ Enter choice(+,-,,/,^,%,#,$): -**
Enter first number: 5 5 Enter second number: 0$ 0$ Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ Enter choice(+,-,*,/,^,%,#,$): #
Done. Terminating
simple beginner calculator
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
choice = input("Enter choice(1/2/3/4): ")
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
break
else:
print("Invalid Input")
prints,
Select operation. 1.Add 2.Subtract 3.Multiply 4.Divide Enter choice(1/2/3/4): 6 Invalid Input Enter choice(1/2/3/4): 1 Enter first number: 34 Enter second number: 54 34.0 + 54.0 = 88.0