
Code
Potion Pricing
Functions get really useful when you can give them values and get answers back.
def add(a, b):
return a + b
total = add(2, 3)
print(total) # 5
aandbare parameters: placeholders listed in thedef.2and3are arguments: the real values you pass in.returnsends a value back to whoever called the function. The calladd(2, 3)is then replaced by that value, so you can store it, print it or use it in more maths.
return also ends the function immediately. Any lines after it
in the body are skipped.
Assignment
- Complete
potion_cost(bottles, price)so it returns the total cost:bottlestimesprice. - Complete
change_due(paid, cost)so it returns how many coins the buyer gets back:paidminuscost.
- Complete
potion_cost(bottles, price)so it returns the total cost:bottlestimesprice. - Complete
change_due(paid, cost)so it returns how many coins the buyer gets back:paidminuscost.
main.py Python sleeping
Press Run to try your code, then Check to test it.