Below is my Code:
def addition(values):
total = 0
for i in values:
total = total + i
return total
def substract(values):
total = values[0]
print(total)
for i in range(1,len(values)):
total = total - values[i]
print(total)
return total
print("Welcome to this code")
print("This code is a simple calculator")
print("--------------------------------")
print("Please select any one")
print("1) Addition\n2) Substraction\n3) Multiplication\n4) Division\n5) Modulation")
choice = -1
while choice < 1 or choice > 5:
try:
choice = int(input("Enter your choice: "))
if choice < 1 or choice > 5:
print("Please enter value between 1 to 5")
except:
print("Enter valid input!")
option = ["Addition", "Substraction", "Multiplication", "Division", "Modulation"]
optionalias = ["add", "subtract", "multiply", "divide", "modulate"]
#print(choice)
#print("You selected " + option[choice-1])
while True:
values = list(map(float, input("Enter values to " + optionalias[choice - 1] + " separated by comma ',': ").split(',')))
if len(values) < 2:
print("Enter atleast 2 values to perform ", option[choice - 1])
continue
if (choice == 4 or choice == 5):
if len(values) > 2:
print("Please enter exactly 2 values for Division/Modulation")
continue
if values[1] == 0:
print("Divisor cannot be Zero (0) !! Please Try again!")
continue
break
operation = {
1: addition(values),
2: substract(values)
}
total = operation.get(choice)
print("Your total is ", str(total))
Please see the result below result
I selected choice as 1, thus my code should only execute the addition function. But this code is executing even the substraction function as given above in my result. Can someone explain if I am missing something?