Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Robert Peters
3,728 Pointshow to post code snippets?!
can someone tell me how they are posting examples of their code on the black background? i can't see a copy/paste button or anything anywhere!
20 Answers

Jennifer Nordell
Treehouse TeacherDown at the bottom of the "Add an answer" is a link to a Markdown Cheatsheet which will explain how to do just that! And it'll explain it better than I can

Robert Peters
3,728 Pointscheers :)

Robert Peters
3,728 Points'''Python def function1(): print("bob") '''

Robert Peters
3,728 Pointseven this i don't understand. i give up

Jennifer Nordell
Treehouse TeacherNo, don't give up! That's never the answer. The beginning marks you have are going the wrong way. We want the ` and you've put '. Now, I don't know where that character is on your keyboard, unfortunately. I do not use a US keyboard. According to what I can find, it should be to the left of the 1 key on an American keyboard. Pressing this key by itself will have no effect so you'll have to press it several times probably to get the three backticks.
Once you do this, you need a blank line after. Then you post your code. Then another blank line. Then three more backticks.
> ```
def function1(): print("bob")
> ```
If you can copy this code without the angle brackets in the beginning, it should result in something that will post your code. Sorry, I was forced to include the > so that it didn't think it was another piece of markdown. Hope this helps!

Robert Peters
3,728 Pointsdef func():
print("well, this is sufficiently embarrassing...")
It's uk but same position, why what type are you on? Cheers been doing my head in feel like a right muppet, must feel like teaching your grandad how to send an email lol, but thanks

Jennifer Nordell
Treehouse TeacherRobert Peters no problem! Just glad you got it working! I have a Swedish keyboard.

Robert Peters
3,728 Pointsmind if i pick ur brain on something else?
got a little python script i ballsed up abit, dunno if ur any good with python?
might as well ask you before making sure someone else pegs me as a dumbass too lol

Jennifer Nordell
Treehouse TeacherYou can give it a shot! I've done some of the python, but I'm not anywhere close to done

Robert Peters
3,728 Points:) i'm sure your much better than me!
It's just from the letter game from the python basics track. that's all i've done yet.
I asked about it before but i couldn't really work out from the answer i got what to actually do and i didn't want to keep asking.
works best offline but still works ok in workspaces.
The problem seems to be that i keep having to send all the variables to each function as an argument, or they won't be recognized. Now ive been told the problem is something to do with making a class, like a grouping of all the variables, and then make some sort of instance of that class. COMPLETELY went over my head and when i looked it up and went through the videos again there was no mention of any of it. Just can't really move on till i understand these basics!!!
I swear i used to be smart lol, honest. Grammar school and everything, but if what's supposed to be a 5 hour track (with the word 'basic' in the title) is taking me weeks and i STILL don't understand, and there's kids just blasting straight through it, ahhh just gets you down.
Anywayyy... if you get a few spare mins later, could you just take a quick look and see if there's any easy way to talk me through what i'm supposed to do?
Thanks alot!!

Robert Peters
3,728 Pointsp.s, please don't judge me too harshly on my choice of material for the game :-p

Jennifer Nordell
Treehouse TeacherWow this is a lot of code! And good job! But I see what you mean about constantly having to pass in the variables everywhere. The reason is this... you haven't made them global anywhere (with the exception of
sboard
). I'm going to attempt to rework your code to show you what I mean, but this will probably take a while

Robert Peters
3,728 PointsJesus you're quick!!! Yeah, sboard i put outside of any functions so that when i went back to the main menu or to other functions, the board wouldn't be wiped etc I just kind of kept making up ways of doing things through trial and error, like the little 'check' loop to get around having duplicated letters messing up the length, but its probably why i messed up so much lol, there's so many different ways to do things it seems!! Awww ur an angel no need to do that! although it may be easier than explaining, don't wurry though no rush, i really appreciate this! :)

Robert Peters
3,728 Pointsjust hard getting started at the moment, ill get there, don't wanna waste your time!

Jennifer Nordell
Treehouse TeacherI had a better idea. I'm going to make a small code snippet to show you the difference between global variables and local variables. But in the meantime... try this. I think you'll find it interesting. Throughout your code you have functions that accept sboard
as a parameter. You also have calls to functions that send sboard
. Here are two examples:
def introduction(sboard):
playagain(sboard)
First make a new file so you can have your original, and copy/paste your code in there. Then start removing sboard
from all of those and see what happens

Robert Peters
3,728 Pointsgot rid of them all i think, im getting this though:
File "game3.py", line 206, in fullguess playagain() File "game3.py", line 143, in playagain introduction() File "game3.py", line 30, in introduction scoreboard() File "game3.py", line 231, in scoreboard print("1st PLACE: ", sboard[0], "\n") UnboundLocalError: local variable 'sboard' referenced before assignment

Jennifer Nordell
Treehouse TeacherCan you link a new snapshot?

Robert Peters
3,728 Pointsdamn, i did it on my sublime text editor lol should have done it on workspaces, when i copy it over the indents are all messed up! Ill do it on here now. 2 mins... maybe it's just because its trying to reference a list item that isn't there yet? i had to create the try block to get around the IndexError anyway, could just add that to the try block?

Robert Peters
3,728 Points
Robert Peters
3,728 Pointsits game3, think it's just the referencing a non-existent list item thing though

Jennifer Nordell
Treehouse TeacherI'm taking a look now. I reworked your game removing the sboard
where they aren't needed. And I also included a file called global.py
. If you run that, it might explain a bit about global variables vs local variables.

Jennifer Nordell
Treehouse TeacherI think you're right. The big point I'm trying to get across here though is that functions can see outside themselves into the global scope. So things that are in the main code are accessible to them. But any variable declared inside the function ceases to exist the second that function ends. And you have the functions sort of doing a chain reaction here. Which actually works, but because those variables and values are going to disappear when the function ends, you have to pass them onto the next function before they go poof . However, if you were to move them to the global score (where
sboard
is) you wouldn't have to pass them every time. Hope this makes sense!
To clarify, you're going to have to send in sboard to the function that is going to manipulate the sboard in some way ie append
. But it's unnecessary to send it into functions that don't need to modify it.

Robert Peters
3,728 Pointshmmmm, only things i don't get about that is why it prints out the 7;
i thought 'return' wasn't supposed to print anything to screen, just save it, and i can't see print(result) anywhere
And why the value of myList has actually been changed;
the list is made (with no values), the function is defined and inside is where a value is added to the list, but am i right to say that the item is not actually added yet? it would only be added once the function is called? but then afterwards i thought it would go back to being empty? That's what seems to happen with my sboard list, now that ive taken it out as an argument to each function nothing gets saved there, but with your script, it prints the last 10 and the list is saved globally, but i can't see why it's been saved globally. GOD it's confusing lol

Jennifer Nordell
Treehouse TeacherTo be fair, I'm not sure why taking out the sboard from that function makes it fail. It should be global and work just like my script, in my opinion.
The 7 is generated by this:
z = myLocal(x, y)
print(z)
Here we're sending in the values 2 and 5. They are added together and return
ed. When that result is returned z is now equal to the value returned. In this case 7. And we print z.

Robert Peters
3,728 Pointsso like your list myList, inside the function it is appended to, but then that should disappear once the function is finished tho right? and yet it still works and prints it, from outside the function, even though the list isn't added to the function as an argument

Jennifer Nordell
Treehouse TeacherAgain, I mean variables that are declared locally. Like for instance my result
variable will go poof. The others, however, are global and won't go anywhere.

Robert Peters
3,728 Pointsahrite, so that code you just posted means both print 10 and then print 7? because z is now equal to the whole function isn't it, which includes the print(z) which is 10 at the time, then the return result which is 7. So.... if you make a variable within a function it just disappears and can't be used, but if you change a variable or append a variable, it stays that way afterwards? seems odd, like why would it be so that you can't use a variable that you have just made, why even make it if you can't use it lol!

Jennifer Nordell
Treehouse TeacherImagine for a moment that you have some sort of print function that prints whatever the user wants the number of times they want it. You could most certainly use a temporary variable for the counter that would iterate through that. There's no reason to have a global counter And no, a local variable (no matter what you do to it) disappears after the function ends. It's what we refer to as "scope".ยด We can send things into the function, and we can return things from the function, but we can't really see into the function if you want to put it that way.

Robert Peters
3,728 Pointsjust quickly, so is the value of z now 7, 10, or the entire function call?
because z = myLocal(x, y)
but within that, z = x * y, so it's 10 now
but then after that you do print(z) and it prints both 10 and 7,
i'm really sorry for this!!
great little example script tho! will have to mess about with it, see if something clicks
I swear, one of these days, probably weeks from now, im going to get past this damn "5 Hour" python basics track if it kills me!!!

Jennifer Nordell
Treehouse TeacherOk let's see if I can walk you through it So we have the main part of my script which is outside the function. When I say main part, I'm not referencing the function.
So we start of by printing x, y, z. So far, so good. Pretty straight forward.
Now here comes the tricky part. We now set z equal to myLocal(x,y)
. But what is that? Well, we don't know. So we send in the values x and y into the function. When they get there, there's two new variables accepting those values num1
and num2
. So the values coming in are assigned in the order we sent them, num1
gets the value of x (which is 2) and num2
gets the value of y (which is 5).
Now every line of code is going to run in that function. And first, we declare a new local variable named z and assign it the value of x * y. Again, because our function can see the global variables it knows to set z equal to 10. Then we print out our z (which is 10).
In our next part we declare a new local variable named result
. This adds num1
and num2
together for a total of 7. But z is still 10 because we haven't changed it. It is appended to myList
.
And now, for the final bit. The return
statement. The return statement sends a value/list/object whatever back to the piece of code that called it. And we're return
ing result, which as we've stated is 7.
So z=myLocal(x,y)
will be the same (in this case) as saying z = 7
because 7 is returned and assigned back into z. Then we print z.
The last thing we do is print all the values in myList
. I sincerely hope I'm not making this worse!

Robert Peters
3,728 PointsGot it working!!!
Exactly like you said, take all the sboard bits out everywhere APART from where sboard is appended to, so the scoreboard function, and when calling that function. Id taken it out of the too which is why it wasn't working.
AAAAHHHHH!! FINALLY i get it, the actual practical side of scope, global and local, been reading wikipedia and had someone on here try and explain it, just didn't get the actual applicable side of it, like how to take someones definition of 'scope' and use that to sort out my script and see where i was going wrong, your a legend! Seems so simple now, like what the hell, how did everyone else get this and not me??!!!! for so long too!! They should add that little code and your explanations to one of the early vids on making functions, don't know if i missed one or something but that was not made clear to me at all.
Bless ya for being so patient lol i feel a right pillock now!
now i just need to work out how to do it the the rest of the 250 lines... and then i can move on!!!

Jennifer Nordell
Treehouse TeacherYou're quite welcome. Keep in mind that you'll have plenty of these light bulb moments where all of a sudden it will finally make sense. And in the end, that's what you're after. And btw... scope is not an easy concept to grasp
But seems like you've got some idea of how it works now

Robert Peters
3,728 Pointslegend

Robert Peters
3,728 Pointsi take it your in Sweden, so 3.30 in morning too.