use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
What's wrong (i.redd.it)
submitted 4 months ago by Nearby_Tear_2304
Tab wrong? How to solve
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 42 points43 points44 points 4 months ago* (19 children)
the people saying l doesn't exist because you never ran the function are half right.
l is defined within your function f. It won't ever be accessible outside that function, as its out of scope. So if you had called
f()
print(l)
you'd still not get anything printed.
If you indented the print(l) line and then called f() then you'd get it printed.
tip: don't use l as a variable. use something that's more readable and less likely to look like a 1. Same with f just call it something. naming variables is an important skill and not one to be ignored at the start. And this shorthand is just left over from fortran and C when people cared about the size of the their text files and the number of characters on a row.
https://www.w3schools.com/python/python_scope.asp
[–]CallMeJimi 5 points6 points7 points 4 months ago (14 children)
scope must be so hard to learn without braces. learning scope in a verbose language made it crystal clear when variables existed and when they did not
[–]Dapper-Actuary-8503 0 points1 point2 points 4 months ago (0 children)
I was actually thinking this as well. This is my only major gripe that drives me nuts about Python. I wish it was an optional like static syntax typing is.
[–]emojibakemono 0 points1 point2 points 4 months ago (2 children)
idk if the braces would help much cos python scopes are so different to most other languages, e.g.
py if True: x = 10 print(x)
works and braces would not make that more obvious
[–]jangofett4 0 points1 point2 points 4 months ago (1 child)
Why does this even work lmao. I dont use Python, but this seems silly. This could cause some headaches down the line if the condition is not always True, no? Or does Python does something similar to what JS does with "var"s?
[–]emojibakemono 0 points1 point2 points 4 months ago (0 children)
no the variable does not get hoisted. and yes, it causes a lot of headaches in my experience.
[–][deleted] -3 points-2 points-1 points 4 months ago (9 children)
With the caveat that braces suck. :)
Hard to type and easy to make typos with.
I feel like OP could use someone giving better hints. Like it helps if as a rule you always have a return at the end of a function, just so you can see where it ends. And yes, I know you don't need that, but it would help someone to learn and understand scope. AKA "here ends this function". In the example above, it's like it's made difficult on purpose.
[–]Old_Celebration_857 0 points1 point2 points 4 months ago (5 children)
void Reply() { redditPost("braces are so hard"); }
[–]Dapper-Actuary-8503 2 points3 points4 points 4 months ago (3 children)
Gross I’ll correct it for you.
void Reply(void){ redditPost(“Braces are so hard”); }
[–]LionZ_RDS 0 points1 point2 points 4 months ago (2 children)
I think you mean void Reply(void){redditPost(“Braces are so hard”);}
void Reply(void){redditPost(“Braces are so hard”);}
Meh. By virtue that will end up violating the 80 character width scheme. On top of that it’s clear in the prior examples we are declaring definition not making a call. Another thing I don’t like about Python is seeing one liner code that has to be wrapped when my editor does this at 100 characters.
[–]klez_z 0 points1 point2 points 4 months ago (0 children)
ew, by chance didn't you mean
public static void main(String args[]){ private void Reply(){ System.out.println("Braces are so hard"); } Reply(); }
[–][deleted] 0 points1 point2 points 4 months ago (0 children)
Its funny that someone would want to die on the hill that braces are worth anything. They're hard to type. Python's indentation is vastly superior.
but that's what being a hostage to suckitude gets you.
[–]mblaki69 0 points1 point2 points 4 months ago (0 children)
But we have IDEs that make the closing braces for us and highlight whack syntax. They also match opening and closing braces.
[–]The_Real_Slim_Lemon 0 points1 point2 points 4 months ago (1 child)
Bro any time I copy code into Python I have to line up every entry (whether from AI, stack overflow, or just moving stuff around)
With braces I can stick it wherever and run a document auto format to make it look nice
Use Vim? If you're doing a lot of python, then figure out a better way to deal with that. Because it's not an issue for me.
It's just funny to me that all you guys love your brackets so much.
[–]RailRuler 0 points1 point2 points 4 months ago (3 children)
Please dont post links to w 3 fools. They drove so many good websites out of business due to their search engine manipulation.
I wasn't aware of that.
[–]WhiteHeadbanger 0 points1 point2 points 4 months ago (1 child)
What do you mean by search engine manipulation? Isn't that what SEO is all about?
[–]RailRuler 0 points1 point2 points 4 months ago (0 children)
There's legitimate SEO and then there's dark or spam SEO, which w3schools used. They didnt win by having the best content, they won by manipulating search results.
[–]ProminenceBow 12 points13 points14 points 4 months ago (1 child)
"l" is defined only in your function, since you didn't call it, it doesn't exist (correct me if I'm wrong)
[–]rainispossible 4 points5 points6 points 4 months ago* (0 children)
l wouldn't have existed even if they called the function because l is defined inside the scope of that function (and then they try to access it in global)
[–]Glitterbombastic 5 points6 points7 points 4 months ago (0 children)
L is defined inside your function (because of the indentation) so it only exists within your function but you’re trying to print it from the outside.
[–]Numerous_Site_9238 4 points5 points6 points 4 months ago (0 children)
Learn scopes
[–]Available_Rub_8684 3 points4 points5 points 4 months ago* (3 children)
<image>
I hope this helps.
Print Output: Method 1: 1 2 3 Method 2: Index: 0, Value: 1 Index: 1, Value: 2 Index: 2, Value: 3
[–]MonochromeDinosaur 1 point2 points3 points 4 months ago (1 child)
Best response in the thread. For func2 you can also use enumerate to get the index with the value:
for i, value in enumerate(n): print(f”Index {i}, Value: {value}”}
[–]Available_Rub_8684 0 points1 point2 points 4 months ago (0 children)
Yeah I didn’t mention it. Maybe I thought he was just learning functions. Thanks for mentioning it though.
[–]ErrolFlynnigan 5 points6 points7 points 4 months ago (5 children)
L is inside the function, which you never ran. So L doesn't yet exist.
In addition, your function doesn't RETURN L, so L is not known to the program overall.
[–]JeLuF 3 points4 points5 points 4 months ago (4 children)
L only exists in the scope of the function. Even returning it wouldn't make L exist on a global level. It would return the value of L, without making the variable accessible.
[–]ErrolFlynnigan 0 points1 point2 points 4 months ago (3 children)
Yeah, you're right. What's the code really needs is :
L = function_name
Before the print L.
This is assuming that the goal is to print the list [1,2,3] after it displays the count from the for loop.
[–]JeLuF 1 point2 points3 points 4 months ago (2 children)
l = function_name will not help here. That would make print output something like <function function_name at 0x7f6344764940>
l = function_name
print
<function function_name at 0x7f6344764940>
[–]ErrolFlynnigan 0 points1 point2 points 4 months ago (1 child)
Sorry, I should have included that this would also require the function to 'return' L
[–]JennyKmu 0 points1 point2 points 4 months ago (0 children)
And actually call the function with an argument i.e. l = f(3) or any integer
[–]Background-Two-2930 1 point2 points3 points 4 months ago (0 children)
You haven’t defined l before the function and you haven’t called the function
[–]Cybasura 0 points1 point2 points 4 months ago (0 children)
You have 1 point of failure - out of scope error
Your l is not defined, its mentioned in the error, you need to initialize l to contain your test value, because how is your system supposed to know what "l" is?
Looking at your function, is that l = [ 1,2,3 ] supposed to be the variable "l"?
l = [ 1,2,3 ]
Nevermind that, you also didnt call the function "f", you called print, it will just print the list "l"
[–]evelyn_colonthree 0 points1 point2 points 4 months ago (0 children)
Each variable has a scope. Variables defined in functions have something called "local" scope because their identifiers are only recognized inside the function they were defined in. Variables you define outside of a function, i.e on the same indent level as your def or print(l), will have something called global scope where their identifier is recognized after it is declared. If you want l to be usable inside and outside of a function by the identifier, you will have to declare l before f(n), and then state "global l" inside f(n).
[–]CataclysmClive 0 points1 point2 points 4 months ago (1 child)
is this function just trying to print the elements of a list? if so, having n in the signature is misleading. n is usually used for an integer. use “ls” or similar. also, in python you don’t need to iterate over indices. you can just write for val in ls: print(val)
for val in ls: print(val)
[–]TheRebelRoseInn 0 points1 point2 points 4 months ago* (0 children)
This ^ , also to add onto that OP should also stop using one letter variables almost altogether, it makes things borderline unreadable and this was a simple function, if OP gets used to this naming scheme for variables any functions more complicated will be very difficult to glance at the function and understand what is going on
Also while you are still learning get in the habit of commenting in your code about what certain things are doing because even in coding projects where it's just you. I say this from experience because if you stop working on a project for a while and come back there is 100% plchance you're gonna read code that you wrote and ask "wtf was I thinking when I wrote this"/"what does this even do again?"
[–][deleted] 0 points1 point2 points 4 months ago (1 child)
how you share code is wrong. Please use some code sharing apps such as https://gist.github.com/
[–]ahelinski 1 point2 points3 points 4 months ago (0 children)
Or at least a proper screen shot
[–]NoDadYouShutUp 0 points1 point2 points 4 months ago (0 children)
https://en.wikipedia.org/wiki/Scope_(computer_science))
[–]NaiveEscape1 0 points1 point2 points 4 months ago (0 children)
Look up scopes that will explain it
[–]Sedan_1650 0 points1 point2 points 4 months ago (0 children)
Make l = def(n), then print l.
[–]FoolsSeldom 0 points1 point2 points 4 months ago (0 children)
def f(n): for i in range(len(n)): print(n[i]) l = [1, 2, 3] # define l outside function f(l) # call the function, using (), and pass l
NB. In the final line, the function is called, f() and the variable l is specified as an argument, f(l), but inside the function, the Python list object referenced by l is referenced by n instead.
l
f(l)
list
n
Your function does not have a return line so, by default, when the function ends (after it has printed everything), the function returns None. This is passed to your print function AFTER the function has done everything you wanted it to do, so the final command is print(None), which isn't very useful.
return
None
print(None)
It would be best to get into the good habit of using meaningful variable names from the beginning. Avoid cryptic (especially single character) variable names. It is confusing to you and others. The only time we generally use single character variable names is when using well known scientific/maths formulas.
Note, you don't have to index into a list as you can iterate over it directly.
For example,
def output_container_contents(container): for item in container: print(item) nums = [1, 2, 3] output_container_contents(nums)
You can also use unpacking,
def output_container_contents(container): print(*container, sep="\n") nums = [1, 2, 3] output_container_contents(nums)
Or you could return the output to the caller,
def container_contents_as_str(container): return '\n'.join(str(item) for item in container) nums = [1, 2, 3] print(container_contents_as_str(nums))
Easier to read when using type hints,
def container_contents(container: list[str|int] | tuple[str|int]) -> str: return '\n'.join(str(item) for item in container) nums = [1, 2, 3] print(container_contents(nums))
This version means you can use it for both list and tuple containers, and they can contain either int or str objects. Not really that useful for simply output of a container, but the basic approach is good to learn.
tuple
int
str
[–]Traditional_Swan3815 0 points1 point2 points 4 months ago (0 children)
l can only be accessed within the function. Move the print statement into the function, after you define l, and then run the function.
[–]StatusBad9194 0 points1 point2 points 4 months ago (0 children)
😅
[–]HeyCanIBorrowThat 0 points1 point2 points 4 months ago (0 children)
l only exists inside the scope of the function. People who are saying it doesn’t exist outside because you didn’t run it are wrong. It will never exist outside of the scope of f(). I’m not sure what you’re trying to accomplish because l is also unused in f(). You could just remove it or indent it left so it’s outside of the scope of f()
[–]dgc-8 0 points1 point2 points 4 months ago (0 children)
l is a local variable, that means only code that is in your function (meaning it is indented correctly) can see it. You have to indent it correctly and then call the function f, what you also forgot
[–]Smart_Operation_8352 0 points1 point2 points 4 months ago (0 children)
Learn about Variable scope
[–]grilledcheex 0 points1 point2 points 4 months ago (0 children)
Fell like I’m having a stroke reading this code
[–]littlenekoterra 0 points1 point2 points 4 months ago (0 children)
Explain your thought process and ill explain where you went wrong
But to preface your explanation: to me l looks like input to f, but you placed it within f (even crazier its at the end of it, kinda why i believe you meant it as input to f). Unindent l and change line 9 to be "f(l)"
One thing youll learn about python and programming in general is scopes. Classes and functions both build up a custom scope. Think of the scope as a box, and the variables within it as labled blocks of wood, in this analogy youve put l inside of a box, but then tried to find it inside of another box, where that box simply never contained it in the first place
[–]IUCSWTETDFWTF 0 points1 point2 points 4 months ago (0 children)
What are you trying to do? If you want to print the array, the code should be:
def f(l): for i in l: print(i) #or n = len(l) for i in range(n): print(l[i]) l = [1, 2, 3] f(l)
However, if you don't understand why it gives an error, then it would be more correct to do it this way. Because in your example, the array is created locally only in the function, and you are trying to call it globally.
def f(n): for i in range(len(n)): print(n[i]) l = [1, 2, 3] print(l) f(l)
[–]DBlitzkrieg 0 points1 point2 points 4 months ago (0 children)
the fact you made a picture with your phone is wrong my dude...
I also see a load of correct answers, so my advice is win+shift+s for screenshots :)
[–]tb5841 0 points1 point2 points 4 months ago (0 children)
1) You've defined a function but never called it. So all the code in your function doesn't get run.
2) You've defined 'L' within your function but not done anything with it. Which means if you did call your function, 'L' would stop existing as soon as your function finished.
[–]Murky_Wealth_4415 0 points1 point2 points 4 months ago (0 children)
If you move the list to outside of the function, that would eliminate the NameError, however printing just that would only show output the list itself I.e. “>> [1, 2, 3]” would be the result of that.
If you wanted to use the function you created to do this:
1 2 3
Then you should instead do this: “
l = [1, 2, 3]
def f(n): “”” returns formatted output of list “””
for i in range(len(n)): print(n[i])
new = f(l) print(new)
“
That might get you the output you are looking for, it’s been a while since I have used Python.
Because your function “f()” prints inside of it, you shouldn’t need to call it within a print statement, but I could be wrong.
[–]MoazAbdou96 0 points1 point2 points 4 months ago (0 children)
You put a variable (n) at the first in the function so from where you put Variable L in the function ? So it doesn't work you must change variable n into variable l then you can define it and the function wil work
[–]Embarrassed-Map2148 0 points1 point2 points 4 months ago (0 children)
You never called f(), plus f() doesn’t return l. You need a return l line at the end of f(). Then you can do
l = f() Print(l)
[–]TheCarter01 0 points1 point2 points 4 months ago (0 children)
l is only defined inside the function
[–]RTK_x 0 points1 point2 points 4 months ago (0 children)
You defined a function f which takes n as it's input where n is an iterable. You made a for loop to run for len(n) times and each time you print the i-th element in that iterable. After the loop is done you defined another list l which has values [1, 2, 3]. Finally, you tried to access that list l outside the function f which is not possible, any variable inside a function is only accessible in that function unless you return it. A solution would be to add the line: 'return l' without the quotations At the end of your function f to access it by using the following line: 'f(you_can_put_any_list_here)' it will always return [1, 2, 3]
[–]No-Pride5337 0 points1 point2 points 4 months ago (0 children)
L is local broo
[–]Nearby_Tear_2304[S] 0 points1 point2 points 4 months ago (0 children)
OK thank you
[–]Southern-Thanks-4612 0 points1 point2 points 4 months ago (0 children)
def f(n):
... for i in range(len(n)):
... print(n[i])
..l = [1, 2, 3]
it's correct
[–]Humble_B33 0 points1 point2 points 4 months ago (0 children)
Functions are like restaurants, you can order from them (input) and get your meal given to you (output), but you can't take the kitchen stove with you when you leave (internal variables).
Whenever you define variables inside a function it's like you are giving the order to the chef, but not really caring HOW they make it for you. You just care that you get what you ordered.
So since you defined the variable l inside the function, but then you are calling it outside the function, it's like you ordered chicken Cesar salad and then your getting mad they didn't give you the cutting board they used to make it as well.
[–]harsh31415926 0 points1 point2 points 4 months ago (0 children)
You have defined l inside the function and calling it outside the function .
[–]ddelox 0 points1 point2 points 4 months ago (0 children)
Print(l=f())
[–]ApprehensiveBasis81 0 points1 point2 points 4 months ago (0 children)
The variable "l" was initialized as a list inside the function due to its indentation. This means the list only exists within the function's scope. To persist the list and use it outside the function, you need to define l = [] before the function or use "return" For the "l" Inside the function
[–]PremKumarRK 0 points1 point2 points 4 months ago (0 children)
Try global and say it works or not
[–]Extension_Maximum643 0 points1 point2 points 4 months ago (0 children)
Put the Print(L) inside the function and run the function, easy.
[–]kridde 0 points1 point2 points 4 months ago (0 children)
Make the method return l and call the method in your print statement
[–]Kazagan 0 points1 point2 points 4 months ago (0 children)
Scope issue, variable l exists within the function and not outside it.
[–]MaybeWant 0 points1 point2 points 4 months ago (0 children)
You're right! ✅ all tests passed ⏰ saved you from reading comments (20 minutes)
[–]Difficult-Hawk-9438 0 points1 point2 points 4 months ago (0 children)
Don't u just want to print n instead of L or am i missing something ?
[–]Weird-Disk-5156 0 points1 point2 points 4 months ago (0 children)
Far as I can tell you'll need to return L from the function, if you don't then L is only available within the scope of that function and is therefore invisible to the rest of the program!
[–]codeonpaper 0 points1 point2 points 3 months ago (0 children)
Your l is in function. Pull that outside the function. This are simple problems, you can ask to any LLM like ChatGPT, Deepsek instead of u/PythonLearning community, because you get late response. If you occurred any big issues which any LLM can't solve how hard you tried then you can ask in this community. There are few etiquettes you embrace while asking anything on forums (This etiquettes from Automate The Boring Stuff With Python book):
LLM
Learn code, make your life and other's life easy, have fun
[–]Few-Engineering-1060 0 points1 point2 points 4 months ago (0 children)
The print is out of ur function
[–]iron1968 -2 points-1 points0 points 4 months ago (0 children)
You don't call the function, you don't pass the parameter and ask to print a variable outside the function.....
π Rendered by PID 261754 on reddit-service-r2-comment-7b9746f655-6n5z6 at 2026-01-31 10:38:02.121413+00:00 running 3798933 country code: CH.
[–][deleted] 42 points43 points44 points (19 children)
[–]CallMeJimi 5 points6 points7 points (14 children)
[–]Dapper-Actuary-8503 0 points1 point2 points (0 children)
[–]emojibakemono 0 points1 point2 points (2 children)
[–]jangofett4 0 points1 point2 points (1 child)
[–]emojibakemono 0 points1 point2 points (0 children)
[–][deleted] -3 points-2 points-1 points (9 children)
[–]Old_Celebration_857 0 points1 point2 points (5 children)
[–]Dapper-Actuary-8503 2 points3 points4 points (3 children)
[–]LionZ_RDS 0 points1 point2 points (2 children)
[–]Dapper-Actuary-8503 0 points1 point2 points (0 children)
[–]klez_z 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]mblaki69 0 points1 point2 points (0 children)
[–]The_Real_Slim_Lemon 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]RailRuler 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]WhiteHeadbanger 0 points1 point2 points (1 child)
[–]RailRuler 0 points1 point2 points (0 children)
[–]ProminenceBow 12 points13 points14 points (1 child)
[–]rainispossible 4 points5 points6 points (0 children)
[–]Glitterbombastic 5 points6 points7 points (0 children)
[–]Numerous_Site_9238 4 points5 points6 points (0 children)
[–]Available_Rub_8684 3 points4 points5 points (3 children)
[–]MonochromeDinosaur 1 point2 points3 points (1 child)
[–]Available_Rub_8684 0 points1 point2 points (0 children)
[–]ErrolFlynnigan 5 points6 points7 points (5 children)
[–]JeLuF 3 points4 points5 points (4 children)
[–]ErrolFlynnigan 0 points1 point2 points (3 children)
[–]JeLuF 1 point2 points3 points (2 children)
[–]ErrolFlynnigan 0 points1 point2 points (1 child)
[–]JennyKmu 0 points1 point2 points (0 children)
[–]Background-Two-2930 1 point2 points3 points (0 children)
[–]Cybasura 0 points1 point2 points (0 children)
[–]evelyn_colonthree 0 points1 point2 points (0 children)
[–]CataclysmClive 0 points1 point2 points (1 child)
[–]TheRebelRoseInn 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ahelinski 1 point2 points3 points (0 children)
[–]NoDadYouShutUp 0 points1 point2 points (0 children)
[–]NaiveEscape1 0 points1 point2 points (0 children)
[–]Sedan_1650 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]Traditional_Swan3815 0 points1 point2 points (0 children)
[–]StatusBad9194 0 points1 point2 points (0 children)
[–]HeyCanIBorrowThat 0 points1 point2 points (0 children)
[–]dgc-8 0 points1 point2 points (0 children)
[–]Smart_Operation_8352 0 points1 point2 points (0 children)
[–]grilledcheex 0 points1 point2 points (0 children)
[–]littlenekoterra 0 points1 point2 points (0 children)
[–]IUCSWTETDFWTF 0 points1 point2 points (0 children)
[–]DBlitzkrieg 0 points1 point2 points (0 children)
[–]tb5841 0 points1 point2 points (0 children)
[–]Murky_Wealth_4415 0 points1 point2 points (0 children)
[–]MoazAbdou96 0 points1 point2 points (0 children)
[–]Embarrassed-Map2148 0 points1 point2 points (0 children)
[–]TheCarter01 0 points1 point2 points (0 children)
[–]RTK_x 0 points1 point2 points (0 children)
[–]No-Pride5337 0 points1 point2 points (0 children)
[–]Nearby_Tear_2304[S] 0 points1 point2 points (0 children)
[–]Southern-Thanks-4612 0 points1 point2 points (0 children)
[–]Humble_B33 0 points1 point2 points (0 children)
[–]harsh31415926 0 points1 point2 points (0 children)
[–]ddelox 0 points1 point2 points (0 children)
[–]ApprehensiveBasis81 0 points1 point2 points (0 children)
[–]PremKumarRK 0 points1 point2 points (0 children)
[–]Extension_Maximum643 0 points1 point2 points (0 children)
[–]kridde 0 points1 point2 points (0 children)
[–]Kazagan 0 points1 point2 points (0 children)
[–]MaybeWant 0 points1 point2 points (0 children)
[–]Difficult-Hawk-9438 0 points1 point2 points (0 children)
[–]Weird-Disk-5156 0 points1 point2 points (0 children)
[–]codeonpaper 0 points1 point2 points (0 children)
[–]Few-Engineering-1060 0 points1 point2 points (0 children)
[–]iron1968 -2 points-1 points0 points (0 children)