Calculator Arm
🦾 So far your bot can only talk. Now it gets arms: tools it can use. A tool is just a function, a named block of code you can run again and again.
def greet(name):
return "Hi, " + name + "!"
print(greet("Sam"))Hi, Sam!
def makes the function. The names in the brackets are its parameters. return hands an answer back to whoever called it. elif means "else if": try the next test. Your first tool: a calculator.
Instructions
- Make a function
calc(a, b, op). - When
opis"+"returna + b, and when it's"-"returna - b. - Also handle
"*"(times) and"/"(divide). - Print
calc(12, 4, "/"). It should show3.0.
💡 Hint
Use if and elif: if op == "+": return a + b, then elif op == "-": and so on. Every line inside the function is indented 4 spaces, and the lines inside each if another 4.