Summary

Resources

Notes\ Code

For our final project today we are making a calculator. We will take the first number → then the operation → then the second number → then give the result. This project is broken into task again, and here our task for this project:

  1. Write out the other 3 functions - subtract, multiply and divide.
  2. Add these 4 functions into a dictionary as the values. Keys = "+", "-", "*", "/"
  3. Use the dictionary operations to perform the calculations. Multiply 4 * 8 using the dictionary.

But before we begin here is some quick code to go over:

def add(n1, n2):
    return n1 + n2

We have seen functions like this before, but we haven't seen the ability to store a function as a value of a variable. To do so we would declare a variable and then put the name of the function without parenthesis. Something like this:

def add(n1, n2):
    return n1 + n2

my_favorite_operation = add
print(my_favorite_operation(2, 3))

When we are calling the variable with the function here, we add the parenthesis back and add our parameters.