Need help improving number guessing game

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 15 08:01:24 EST 2008


On Mon, 15 Dec 2008 01:06:51 -0800, feba wrote:

> I don't really understand dicts yet; actually, the tutorial I'm
> following (http://www.briggs.net.nz/log/writing/snake-wrangling-for-
> kids/ , designed for tweens, but other than the pointless anecdote
> and joke here and there, I've found it a very good guide) doesn't
> even seem to mention them, from a search of the pdf. Actually,
> apparently I stopped and started working on this just before the
> chapter on functions and modules.
> 
> I'll look into that later on, but for now I'm pretty happy with how
> it works.

Dicts, or dictionaries, also known as "hash tables" in some computer
languages, are a mapping from a key to a value. Think of looking up a
word in a real dictionary: the word is the key, and the definition is
the value.

Imagine a game with multiple players, each known by their name.
Because you don't know how many players there are, or what their
names are, you can't do this:

fred_score = 0  # How do I know there's a player called Fred?
barney_score = 0
...

But you can do this:

names = get_players_names()
scores = {}  # start with an empty dict 
for name in names:
    # the player name is the key, and the score is the value
    scores[name] = 0
print scores

=> {"fred": 0, "barney": 0, "wilma": 0, "betty": 0}

(or whatever names you have been given).

Later, you want to print Fred's score:

print "%s's score is %d" % ("fred", scores["fred"])

will print "fred's score is 0".

You might need to add 1 to Wilma's score:

score["wilma"] += 1

And so forth.


[...]
>>You should either put this in it's own function (could be
>>named 'main'), or at least "protect" it with an "if __name__
>>== '__main__':" test. 
> 
> Could you go into a bit more detail on this? I don't understand what
> should be its own function, nor do I understand what that line would
> do or how to use it.

Consider a really simple Python module:

# module.py
def hello():
    print "Hello parrot!"

print "Running the module"
hello()
# end module.py


If you execute that file, from the commandline or the desktop, it
prints

Running the module
Hello parrot!

just as you expect. But when another Python module imports it, using
the command "import module", not only is the function hello() loaded,
but the two lines above are printed too -- but only the first time
you import the module. This is usually not what you want.

Generally, you want the *execution* to be separate from the
*importing*. There is a way to do this is Python:

# module.py
def hello():
    print "Hello parrot!"

if __name__ == "__main__":
    print "Running the module"
    hello()
# end module.py

When you import the module, Python sets the special variable 
"__name__" to the string "module". It's the name of the module. But
when you are executing the file from the command line, Python sets
the special variable to the magic string "__main__" instead. So the
code inside the if __name__ block is only executed when you are
actually executing the module, not when you import the module.

Hope this helps somewhat.



-- 
Steven




More information about the Python-list mailing list