Quick actions

cmd+k|ctrl+k

Navigation

Languages

20240415 Calculator Function

Snippet info

Language

Python

Visibility

public

Author

maxine-chiu

Created

2024-04-15T01:59:49.757625Z

Updated

2024-04-15T02:37:20.976873Z


"""1  **Calculatior Funciton**:
Write a Python funciton that takes two numbers and 
as input and returns the result of the operation"""

def calculator(num1, num2, operator):
    if operator == '+':     # check for +
        return num1 + num2 
    elif operator == '-':   # check for -
        return num1 - num2
    elif operator == '*':   # check for *
        return num1 * num2  
    elif operator == '/':   # check for /
        if num2 == 0:  # make sure num2 is not 0
            return "Division error"
        else: # num2 is 0 
            return num1 / num2
    else : # opeartor is not +-*/
        return "error : invalid operator"
result = calculator(5, 3, '+')
print(result)
INFO