[Tutor] Re: Please critique my guessing game program.

Magnus Lyckå magnus at thinkware.se
Fri Jul 16 22:59:14 CEST 2004

At 16:45 2004-07-16 +0100, Matt Smith wrote:
>I've changed the program further to account for the help and suggestions
>I've recieved.  I think this version is structured in a more logical way
>and should also be impossible to crash through inputing the wrong answer.
>Please let me know what you think and if the program could be improved in

It seems redundant to use both "playagain" and "playagain"

>I have greatly enjoyed writing my first Python program.

I hope you will enjoy writing a lot more programs soon!

>playagainflag = 1
>while playagainflag == 1:
>    playagain = raw_input("Do you want to play again? (y/n) ")
>     if playagain == "y" or playagain == "Y":
>         playagainflag = 1
>     else:
>         playagainflag = 0

I'd just do...

playagain = 'y'
while playagain.lower() == 'y':
     playagain = raw_input("Do you want to play again? (y/n) ")

...and skip all the use of that flag variable.

Another solution which is seen fairly often in Python is to do
it like this:

while True:
     if raw_input("Do you want to play again? (y/n) ").lower() != 'y':

It might possibly be prettier to make that into...

while True:
     if stop_playing():

...and define stop_playing as a separate function with the raw_input
or whatever. But that really only makes sense if the rest of the
actual user interaction is removed from the logic as well.

Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language 

More information about the Tutor mailing list