Scope
Python · Unit 4: Functions
- Full plan
- Python
- 9 exercises
The variables you create inside a function are local: they are born when you call it and disappear when it ends.
Outside they don't exist. What does come out is the value of the return.
def calculate():
total = 5 * 3
return total
result = calculate()
print(result)Prints
15
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Find the bug
This program should print the price of 100 with the discount. Which line has the error?
2. Predict the output
What does this code print?
3. Multiple choice
Where can the variable
totalbe used?4. Predict the output
What does this code print?
5. Complete the code
Fill in the blank so it prints what 3 tickets cost.
6. Predict the output
What does this code print?
7. Find the bug
This program should add a deposit of 5 to
balanceand then print the new balance: 15. Which line has the error?8. Put the lines in order
Put in order to print the total of 70 plus 5 for shipping.
9. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.