ACTIVITY 38 / 50
Try as many times as you needWhere a variable lives
Let’s understand
A variable created inside a function normally belongs to that call. It is local. The same name outside may hold a different value. Changing the local value does not automatically overwrite the outer one. Use return to send a calculated result out. Here we deliberately reuse a name to make the boundary visible.
Listen to the explanation
Example
name = "base"
def report():
name = "rover"
return name
print(report())
print(name)Keep energy = 100 outside. In test, create local energy = 3 and return it. Print test(), then the outer energy: 3 and 100 on separate lines.
I need a hint
Define def test(). Do not indent the outer variable.
How to find an error
- Read the last line of the error message.
- Find the indicated line. Check quotes, parentheses and indentation.
- Change one thing and run again.
main.py
4 spaces
Your draft stays in this browser.
Result
PythonYour program’s output will appear here. Click Run.
Мой профиль