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 PointsLooking for some basic guidance/help with my hangman game script from the python basics course
I have created a little hangman game, works better offline as the console on here is annoying and hasn't got a scroller, but is still ok. I have added several little ideas like a score and scoreboard, and the ability to guess part way through for extra points etc, but i get the feeling ive missed/messed up on a few of the real basics, like adding arguments to functions. was just hoping some more experienced people could take a quick look and give me some advice? The main script is 'game2.py' and theres a script that accompanies it, containing the words and comments etc called 'data.py'. The snapshot is https://w.trhou.se/42qpgej6vs Btw, id probably read the 'instructions' page for a good grip on what's going on, as (to me anyway) it all seems very cryptic! MANY thanks for any help you can provide! I really need to get these basics out the way really before i can move on to the rest of the python tracks and i just don't think ive got it all yet, i'm guessing there will be quite a few unnecessary bits in it!
8 Answers
Kristian Gausel
14,661 PointsThere are many ways to improve this code (dont feel sad about this, one could say that about all code). But my main concern is that you don't know your scopes. So you should try to read up on how Python scopes it's variables.
Another thing you seem to do is to make more classes and less variables. Maybe make a session class which represents one game, then you only have to pass an instance of this around, instead of passing around "good scores, bad scores" and so on.
Recommended reading is therefore: Scopes and Classes Start with that and happy coding
Robert Peters
3,728 Pointsunfortunately the wikipedia page just blew right over my head, but cheers tho, ill try again on here for abit, see if anything clicks. as far as i can tell tho a class is like a group of variables and a scope is where they are stored. was i right to think the problem was to keep putting the variables in the function names with each function? because the chap in the vid seemed to, and if i take them out then it doesn't work, says that they aren't recognised (or something similar). on the upside, im getting an output not just error messages. not that i see how making text appear on a terminal is going to translate into anything graphical like a software program or website lol
Kristian Gausel
14,661 PointsYes you can think of a class as a group of variables and methods (functions) that mutate (change the variables) on an object (an instance of a class).
In python everything is objects. Which is why you can use
"Hello world".upper ()
"Hello world" is an object (instance) of the string class. And upper is a method of the string class. Strings might be a bad example because in python they are immutable meaning they cannot be changed after instantiation. Which is why upper () returns a new object of type string with the desired content. Instead of mutating the current one.
The reason python could not find your variables when you didn't include them in the method signature, is because the were out of scope. A.k.a unreachable in the current context.
You are right that you should pass the information along with to the functions. But you should group them in a class. That way you can get away with only passing one object to the function. Namely the object containing all the current games data. (PS you can also make everything into methods on your game class and thus not need to pass any data in the method signature.)
As for your last statement. You could redirect stdout to "print to apache" (a webserver) such that you can output it to a website. And python has many tools for webdevelopment. While I agree that printing can't be turned into a game, you can try something like pygame if you want 2d graphics ;)
Python can do anything!
Robert Peters
3,728 Pointsso functions are called methods in python (or are called both), and what i've been doing is creating lots of methods, and passing variables between them, whereas i should have grouped all of the methods together in something called a class, meaning that i don't have to pass around any variables, because they are all part of the same 'object' (class) and therefore all the different methods i created have knowledge of all the variables within the other methods too. And an instance is like... the entire class at a certain moment in time? anywhere close? i know this must be very frustrating but im just so confused, and i don't know where to go from here as the python docs/wikipedia pages are years ahead of me, and i don't want to keep going on here without even knowing these basics. as far as i can tell everyone else just blasted right past, and the 5 hour basics track has taken me weeks and i still don't get a thing. very depressing. i used to be smart, grammar school kid, and now at 25 i cant get to grips with something that an average child can probs learn in a day or 2. is there like a page somewhere that just goes over a few basics that even a retard can understand?
Kristian Gausel
14,661 PointsWell, Usually I refer to functions belonging to a class for a method. But it is essentially the same thing, like you're saying.
An object is an instance of a class. You can view the class as the blueprint, and the object the thing produced according to that blueprint. There may be thousands of products made from the same blueprint, but they all have the same features. They can have different colors, sizes and prices, but they are all the same thing. (In this example colors sizes and prices would be attributes on the class (blueprint))
Don't be discouraged though. I suggest just continuing on the tracks here at treehouse and see where that takes you. It is also slightly difficult for me to explain everything, as all of the terms I use to describe stuff might feel a little advanced, when in fact the idea is usually quite simple.
Robert Peters
3,728 Pointsthat was all wrong wasn't it.
ok so ive started to get the hang of scope, how variables defined locally are lost after the function is finished, but variables defined globally are usable and alterable from within a function (can be seen locally) etc.
I'm just wondering how i would go about creating this class, that stores all the variables for global use, and then how i would pass that on to the functions.
(Find it hard to visualise how this would be done sorry!)
Is any of this actually covered in the python basics track? i think ive missed something!
Robert Peters
3,728 Pointsim sorry i know its a pain but im determined to get it before i move on, im going to learn to program if it kills me, ive decided
Kristian Gausel
14,661 PointsSo, I didn't spend much time on this, but this is one way you could alter it to be class based:
Note that this could be done much better, but I just copied and pasted your code basically.
Which also reminds me that I should tell you that your code just dives deeper and deeper into the stack frame, which I think eventually will crash python, as I seem to remember that python's max call depth is 500. Not that you will ever notice this in any use case you encounter. You can see this by putting this right under import sys, and then guessing a few times:
sys.setrecursionlimit(20)
To understand this however you need to read more up on computer architechture and stacks. (My tip is to ignore this for now, but keep it mind for a later date). I tried to fix some of the most obvious places where you should return instead of call the function you want to go to. But again, if you dont understand this, dont care too much about it.
Robert Peters
3,728 Pointswell, that's pretty awesome! Makes alot of sense doing it that way actually! Simply because the much more (i want to say modular?) syntax, and use of the 'return' function (method or woteva lol) means that instead of going into functions within functions that call other functions then call that same function from within itself etc etc, (which i think is what you mean by a stack frame?) a function is called, returning a value to the calling function/variable, so the (subprocess?) function is ended. I.e only one function.
PLEASE say that's close haha! or along the right lines!
Im absolutely certain that wasn't in the track though (creating a class), so maybe im being well too hard on myself!! i thought i should know all this by now lol, and have been cursing the "5 hours" estimated time with a vengeance!
id managed to get somewhere last night anyway, and i did this:
basically just removing most of the variables etc to outside of the functions, and using the function 'global' on the rest or when i need to create/change a variable, although i know this is supposed to be bad practice (but i don't know why), and so i was then going to start looking into the use of 'return', however it would have taken me a LONG time and probably still would have ballsed it up, so that example of yours is just brilliant!!!
Thanks man, will give it a good studying today/tonight and move on with my coding!!
Aaaahhh... feels good
Cheers for the help pal i bet you were DYING to tell me to go f...
Kristian Gausel
14,661 PointsNo one ever forced me to reply to you. I think it is great that you want to learn programming. And like you are saying, it is probably not covered in the introduction to python.
Let's not talk about stack frames, as they are pretty complicated for beginners. (http://stackoverflow.com/questions/10057443/explain-the-concept-of-a-stack-frame-in-a-nutshell)
You can view it as a subprocess like you're saying, albeit a little confusing, since subprocesses is another concept altogether. (https://docs.python.org/2/library/subprocess.html)
I am sorry for being so hard on you when it comes to terminology, but it also helps a lot to have the terminology right when you want to google things ;)
If you ever need help, just pm me, ill be glad to assist. Cheers
Kristian Gausel
14,661 PointsA few more comments: 'global' is not a function, it is a keyword. (A function takes arguments, like so: function(argument1 argument2), this is also a difference in python 2 and python 3, the print statement was changed to a function, which is why
print "this works in python 2 but not python 3"
print ("this works in python 2 and python 3")
)
It is considered bad practice to have a lot of global variables because it quickly gets unmanagable in larger programs. Because you quickly lose track of which functions modify what variables and when. This is why you encapsulate (you can google this word for more information) the data instead of making everything global. However, it is also important to note that even if some people call something bad practice it can still be a good or viable idea to use it nontheless.
When it comes to stack frames, you might already know that if you call a function the variables defined inside that function are temporary and private to that function only (scope again). This is because in the underlying implementation you create what is called a stack frame, containing all those local variables. Every time you call a function inside another function, you create a stack frame inside that stack frame. As you can imagine, this can go pretty deep. When you return from a function, the current stack frame is collapsed [and the return value is pushed to the top of the stack.](the information in the square brackets is a topic in and by itself). But since you just keep calling functions inside functions, the loops you create are technically unnessesary, because you just create new stackframes (this is what is known as recursive functions). Python does not optimize for tailrecursion (again, you shouldn't even attempt to google this at this point), which essentially means you keep building those stack frames, and the limit is set to 500 of those stack frames inside eachother. (I think 500 is the default). Fixing this in your game would require some careful thinking and rewriting though, so don't bother. But it is nice to know that you should be careful with highly recursive functions in python. That was very technical, but I know I find information like this super interesting, so I thought I'd share.
The most important piece of information is the second paragraph though ;)
Cheers
Robert Peters
3,728 Pointsi know yeah and thanks for doing so!!
i think it's a travesty that we weren't taught programming in school!
Atleast now they have this micro:bit thing for kids etc, still don't think our actual syllabus is very good though when it comes to tech (UK)
Nah it's alright man i need to know it was just getting annoyed at myself my this is hopeless attitude must have made it worse, lesson learnt!
cheers pal no doubt i will have to in the future at somepoint! Bob
Robert Peters
3,728 Pointsor i would if i knew how anyway!!
Robert Peters
3,728 Points2 from Sandnes, 1 from Stavanger, 1 from simply Norway
Robert Peters
3,728 Pointsi put a snake as my pic if u add me, just my name pal
Robert Peters
3,728 PointsRobert Peters
3,728 Pointslol, the worst bit is that i thought i was getting somewhere, but i guess i shouldn't have been put off asking questions for fear of seeming dumb, as the fact that i literally don't know what your talking about is just depressing. im not sure what a scope is, or a class, and that probably means im even wrong about what a variable is doesn't it? also an instance, not sure ive come accross that either. you think it's just a 'not in my mental capacity' sort of thing or is it worth starting again?
Kristian Gausel
14,661 PointsKristian Gausel
14,661 PointsI did not mean to scare or discourage you. Absolute anyone can learn to program. However it took me years to figure out all by myself, simply because no one ever told me what to read up on. Maybe you should finish some more tracks here on treehouse, that will at least explain what a class, and what an instance of a class is. You can read about scope on wikipedia: https://en.wikipedia.org/wiki/Scope_(computer_science) although it is a little tad technical. But you also learn about scopes from working with classes.
Google everything, and read a lot. Understanding programming might be a long road, and one that feels like it never quite finishes, as you learn new things all the time. But you can definitely do it. Also I definitely reccommend Python as a first language. Good luck!