#๐Ÿ”’ variable not defined?

42 messages ยท Page 1 of 1 (latest)

tulip brook
#

I want to start with a as 100 and it goes down by 10% every time it turns (for loop)

mortal swiftBOT
#

@tulip brook

Python help channel opened

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.

tepid sandal
#

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.

tulip brook
#

oh

tepid sandal
#

A def can reach outside of its own scope to the a in the global scope. That's fine.

tulip brook
#

ok that makes sense

tepid sandal
#

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.

tulip brook
#

thank you so much

#

it now runs as I wanted it to run

tepid sandal
#

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.

tulip brook
tulip brook
#

but looking forward to it

storm heath
# tulip brook ok that makes sense
>>> 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
tepid sandal
#

!e ```py
def func(obj):
print(obj)

func('Hello, world.')```

mortal swiftBOT
#

@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

Hello, world.
tepid sandal
#

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)```

mortal swiftBOT
#

@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 1
002 | 2
tepid sandal
#

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())```

mortal swiftBOT
#

@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

Hello, world.
tepid sandal
#

The object given to be returned replaces the call to the function.

tepid sandal
#

This is called evaluation. func() evaluates to 'Hello, world.'

#

!e ```py
def func():
print('Hello, world.')

print(func())```

mortal swiftBOT
#

@tepid sandal :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Hello, world.
002 | None
tepid sandal
#
def func(parameter):
    pass

func('argument')```think
```py
def func():
    parameter = 'argument'```
mortal swiftBOT
#
Python help channel closed

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.