[Tutor] Re: Please critique my guessing game program.
project5 at redrival.net
Wed Jul 14 13:20:54 CEST 2004
Matt Smith <smith-matt <at> tiscali.co.uk> writes:
> I just started reading about python a couple of days ago and have just
> written my first program, a guessing game (project suggested b o by the
> Python Bibliothecha). I intend to extend the program to make it more
> user friendly but I'd like to get some initial feedback to catch any
> poor programming or bad habits as soon as possible. Here's the program:
Here is a session with the program, which illustrates the main problem:
What is your first guess? asdf
Traceback (most recent call last):
File "temp.py", line 38, in ?
guess = input ("What is your first guess? ")
File "<string>", line 0, in ?
NameError: name 'asdf' is not defined
No error handling! See the discussion started by Dick Moores, it contains
examples on how to properly handle users who don't play nice with your program
(you should generally assume that your users are A) malicious or B) stupid ;),
so don't trust them to give you valid input).
Another thing is the use of tabs for indentation. Although it's perfectly valid,
the preferred style is to use 4 spaces per indentation level. You can set up
your editor to automatically convert a Tab press to 4 spaces. Taste plays a role
here, but IMO it's better to just stick with the preferred style even if your
personal bias is towards tabs.
I'd also suggest using if __name__ == '__main__': at the bottom of the program
so it only automatically runs the game if the script is started standalone (as
opposed to imported in a different program, e.g. when you write a collection of
games accessible from a menu). You'll have to rewrite the initialization
(probably just put it inside Game()) in order to make the game usable from such
More information about the Tutor