def calculator(operator, num1, num2): if operator == "+": result = num1 + num2 elif operator == "-": result = num1 - num2 elif operator == "*": result = num1 * num2 elif operator == "/": result = num1 / num2 else: raise ValueError("Invalid operator. Supported operators are +, -, *, /") return result result = calculator("+", 4, 8) print(result) result = calculator("-", 7, 4) print(result) result = calculator("*", 8, 7) print(result) result = calculator("/", 70, 7) print(result)
#simple calculator witha all functions- def calculator(operator, num1, num2):
- if operator == "+":
- result = num1 + num2
- elif operator == "-":
- result = num1 - num2
- elif operator == "*":
- result = num1 * num2
- elif operator == "/":
- result = num1 / num2
- else:
- raise ValueError("Invalid operator. Supported operators are +, -, *, /")
function = 'add'number1 = 2number2 = 8- return result
- result = calculator("+", 4, 8)
- print(result)
- result = calculator("-", 7, 4)
- print(result)
if function == 'add':sum = number1 + number2print(sum)elif function == 'substract' or 'minus':diff = number1 - number2print(diff)elif function == 'multiply':product = number1 * number2print(product)elif function =='divide':answer = number1 / number2print(answer)else:print('No related function')- result = calculator("*", 8, 7)
- print(result)
- result = calculator("/", 70, 7)
- print(result)