-1

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?

  • The spelling of `subtraction` is clearly the problem! ;-) At least it is consistent in this code. – wallyk Nov 13 '21 at 19:29
  • `subtraction(values)` is executed when building the dictionary `operation`. – mkrieger1 Nov 13 '21 at 19:30
  • In your own words, when you write `1: addition(values),`, why do you expect this not to call the function? – Karl Knechtel Nov 13 '21 at 19:30
  • 1
    There are fundamentally two questions here: "why is my function called when I write this syntax (because I want to bind the parameters)?" and "how do I bind the parameters instead?" I have linked duplicates for both. If you don't actually want to bind the value ahead of time, then the first linked duplicate is sufficient. Just in case you didn't understand: when you write `def addition(values):`, the `(values)` part is **not** part of the function's *name*. – Karl Knechtel Nov 13 '21 at 19:32
  • As others pointed out, you call the function when you put (). But there are languages where you can do exactly what you did. It's called lazy evaluation. Haskell is one such language. – Superior Nov 13 '21 at 19:48

1 Answers1

2
operation = {
    1: addition(values),
    2: substract(values)
}

is defining a dict where the values are the result of calling addition and substract (note: the correct spelling is subtract, no second s). You need to make the values functions to call, not the result of called functions, e.g.:

operation = {
    1: addition,  # No call parentheses, so it stores the function itself
    2: substract
}

total = operation[choice](values)  # Look up the function to call and call it
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271