Need help improving number guessing game

James Stroud jstroud at
Sun Dec 14 04:27:51 CET 2008

feba wrote:
> This is what I have so far. better? worse?

Much better. I didn't check if it works. But you need to figure out a 
way to give up on your reliance on global variables. They will end up 
stifling you in the long run when you move to substantial projects.

Also, you should start moving the nested if: then: blocks to functions.

Finally, you should limit your lines to no more than 80 chars. I do 71. 
Don't be afraid to use transient variables to help.

Be assured that it takes on special intelligence to write unintelligible 
code at will. Also be assured that fitting everything on one line is not 
a good way to save time.

Aim for clarity.

If you want to start off on the right foot, you'll grind on your 
guessing game until all variables are insulated within local scope and 
the logic of the program becomes apparent from its architecture as a 
collection of functions.

This is an example of how to combine these concepts:

def update_scores(player, p1sc, p2sc):
   if player == "P1":
     plsc +=1
     p2sc +=1
   tmplt = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s"
   print(tmplt % (player, p1sc, p2sc))
   return p1sc, p2sc

p1sc, p2sc = update_scores(player, p1sc, p2sc)

Finally, put comments on their own lines. Google "python style guide".


More information about the Python-list mailing list