#๐ I am trying to learn how functions work.
24 messages ยท Page 1 of 1 (latest)
@tulip orchid
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.
def SC():
if Clevel_5 == True:
strength = 5
elif Clevel_4 == True:
strength = 4
elif Clevel_3 == True:
strength = 3
elif Clevel_2 == True:
strength = 2
elif Clevel_1 == True:
strength = 1
the strength value is seen as unreachable.
Maybe show the larger code, and what "shows it as unreachable".
Basicly, strength is a local variable. It will not be visible outside the function.
Any variable you assign to in a function is local by default.
THATS HTE WHOLE CODE SIR
IT TELLS ME THAT ITS NOT REACHABLE IF I PUT IT IN THE FUNCTION
Python functions make any variable you assign to local by default:
x = 1
print(x) # prints 1
def f():
x = 2 # local, _unrelated_ to the outer x
print(x) # prints 2
f()
print(x) # prints 1
if all your function does is assign a value to the local strength variable, what is its point?
That's not "unreachable", it's saying you're setting strength and then never using it.
I am.
For how your code is presently structured you will need to use the global statement. Once you figure out why strength is unused.
Even outside a function, PyLance would complain the same way about this:
x = 1
because i never do anything with x.
how do i use the global statement exactly?
Inside the function, put:
global strength
That say you're be ing the strength varoable from the main code, that it isn't a local variable.
Any of the main code variables you need to change will need marking as global:
global strength, Clevel_2, ........
assuming any of these get changed.
If you're not assigning to them, you don't need this.
!close
This help channel has been closed. 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.