#๐ variable not defined?
42 messages ยท Page 1 of 1 (latest)
@tulip brook
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You're knocking on scope's door.
Also, 0.9 instead of 0,9
Scope is the idea that when you're in a def, the def is its own little world of variable names. The a inside a def is not the same a as outside a def.
oh
A def can reach outside of its own scope to the a in the global scope. That's fine.
ok that makes sense
But when it comes to assignments, it's going to try to assign inside itself.
By trying to traverse outside its own scope and assign within the scope, you're creating a conflict.
Either set up the 100 var inside the def or pass it in as argument to a parameter of the def, which you'd have to add.
Look up the LEGB rule for more information on scopes.

I havent learned argument yet
but looking forward to it
>>> global_var = 10
>>> def func():
... global_var = 20
... print(f'global_var inside of func(): {global_var}')
...
>>> func()
global_var inside of func(): 20
>>> print(f'global_var after func(): {global_var}')
global_var after func(): 10
!e ```py
def func(obj):
print(obj)
func('Hello, world.')```
@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello, world.
Here, obj is a parameter of func. obj can be thought of as a variable that gets assigned to 'Hello, world.'
But within the function's scope.
The argument, 'Hello, world', is provided in parentheses of the call to the function.
There are many ways to design functions' parameter configuration.
!e ```py
def func(a, b):
print(a)
print(b)
func(1, 2)```
@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 2
You can have multiple positional parameters.
Functions also have an outbox. A return.
All functions that complete, return.
If nothing is explicitly given to be returned, None is returned.
!e ```py
def func():
return 'Hello, world.'
print(func())```
@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
Hello, world.
The object given to be returned replaces the call to the function.
print(func())
print('Hello, world.')```
This is called evaluation. func() evaluates to 'Hello, world.'
!e ```py
def func():
print('Hello, world.')
print(func())```
@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Hello, world.
002 | None
Let me know if you have any questions.
def func(parameter):
pass
func('argument')```think
```py
def func():
parameter = 'argument'```
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.