Thursday, August 4, 2011

scumbag python

Ok wow.

So, you have to say:

global wumpus_room

BUT you can't declare it as global AND assign it at the same time.

Oh, and the kicker? You're after-the-fact declaring at as a global variable INSIDE the function you want to modify it in!

So:




I guess by "declaring" inside the function that it needs to be on the lookout for a outside-declared instance of wumpus_room that this makes it ok to alter. This does NOT AT ALL explain why I can see the initial value of the variable as defined outside of the function.

Is "global" only necessary to MODIFY a variable outside of the scope... but not read the value? Doesn't that make the concept of "scope" twisted?

EDIT: Here's the site I got from my google searching on the subject http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/

Python woes



Woof, I don't understand variable scope in Python.

I have what I thought was a global variable called "wumpus_room" that is a random whole number between (and including) 0 to 3. The script is borking at the fact that I have a line that could alter this variable inside of a function. If I comment out the block starting with

elif choice.lower() == "shoot north":

then there is no complaint.

This would not be confusing if it was crabbing about trying to alter a variable out of scope, but it gives a cryptic error:

UnboundLocalError: local variable 'wumpus_room' referenced before assignment

See.... I'd BELIEVE that if not for the fact I can comment out that reassignment line and everything works fine - this tells me the variable is indeed getting assigned!

How the heck do I declare a global variable and then edit that variable if necessary?!

Wednesday, August 3, 2011

LPTHW Exercise 35 and 36

LPTHW exercise 35 brought home a lot of function and control structure concepts with an example of a small text adventure game.

http://learnpythonthehardway.org/book/ex35.html

Exercise 36 asks that you take what you learned and make one of your own. I'm doing basically a five room "hunt the wumpus" game. The structure is basically every room has its own function, and each room asks which way you want to go or where you want to fire your arrow which then either calls the respective room's function, or the fire arrow function. It's not a "proper" way of organizing data for a game but it's just to drive home functions and control structures.

This really brings me back to middle school doing some QBasic programming along the same lines. I have a really strong grasp of control structures because of all the nested if-loops and while-loops of the text adventure games my friends and I used to make together. It sucks that ASCII graphics is now really hard to do since terminal interactivity is now considered an anachronism (among most folks, anyways).

The Python documentation makes no sense to me

Here's how a Python class function (method?) prototype is presented:

class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

I read this as tzinfo is an argument of microsecond which is an argument of second which is an argument of minute which is an argument of hour which is an argument of day. But this isn't the case. It's just a list of arguments that can be passed separated by a comma. So... why the brackets?!

Furthermore in THIS class function the non-bracketed variables are stated as required, and the bracketed ones are optional. So I say to myself "Ah, that's how they define optional and non-optional arguments. However, here:

class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])

the documentation says that all arguments are optional, which blows away that theory of operation.

So yea, the biggest hindrance to getting to the next level in Python is that I can't read the documentation.

labels

Huh, I guess I can tag my posts now. That's cool.

Tuesday, August 2, 2011

even more progress

I'm on exercise 27 now in LPTHW. I think I've spent a total of four or five hours on it. It really helps already being familiar with C since I don't have any issues comprehending the "advanced" stuff so far (functions, return values, taking arguments, importing functions from other files). I'm still weak on some of the formatting like %d and %s and %r in the print statements, but basically it's numbers, strings, and a different way of formatting string statements.

a little progress

I got through the first five exercises in LPTHW, which isn't saying much since it's just typing out the examples (no copy/paste is the rule in this book) and it only took about 45 minutes (including setting up the environment). So far It's all just printing and some light variable usage.

Here's where I stopped: http://learnpythonthehardway.org/book/ex5.html