[Tutor] Re: Here is my program that I made

Andrei project5 at redrival.net
Mon Aug 23 19:58:15 CEST 2004


Kevin wrote on Sun, 22 Aug 2004 16:14:47 -0400:

> Ok I have attached the two files that go together. There are two 
> problems that I can't figure out. One when you start the program it will 
> ask you to enter  choice. When you enter a letter it will rais the 
> exception error, but if you enter a number other then 1 or 2 it will 
> just keep asking to make  choice. The second problem is when you go 

Yes, because there will be no exception. The exception if you enter a
letter is because int(<letter>) can't be executed and therefore causes an
exception. On the other hand, if the user chose "38372", int(38372) will
not fail. The program will check if the input is equal to 1 or 2 (it
obviously isn't) and when it finds out it's neither, it's already at the
end of the loop. So it goes back and starts over. You could do something
like this:

  if int(enter) == 1:
      ....
  elif int(enter) == 2:
      ....
  else:
      <error message - invalid number>

> though the game when you guess the right number it will ask if you want 
> to play again, so you need to enter y or n if you enter y you will start 
> to play again if you enter n it will say thank you for playing then ask 
> would you like to play again if you hit anything it will just keep 
> asking you if you would like to play again. Any suggestions on how to 
> fix that?

Eek, that is a small, but rather scary bit of code you've got there :).
game calls YesNo which in turn can call game which in turn can call YesNo
ad infinitum. That's really not the way you should do it. 
YesNo should return a value: either a True or a False (True if play again,
False if quit). It should not call game()! Let's think about the code
blocks a bit first. We have:

1. a menu system. This one should only launch the game and quit the
program.
2. a game. This should ONLY play a game. Not pretend it's a command
interface. In other words, it has no business calling YesNo.
3. a message dialog (YesNo). 

Now what should happen is this:

The menu starts the game with a function call game(). When the game is
finished, Python is back in the menu and the menu asks whether to play
another game - by calling YesNo. If the user chooses Yes, the result of the
function call will be True and the menu simply launches game() again. This
way you don't have functions calling each other and confusing you.

Three additional tips: 
- don't use '\r\n'. '\n' is enough, even on Windows - Python automatically
converts it to \r for you and you end up with cross-platform compatible
code (\r\n is pure-Windows).
- don't split such small programs into modules. It's more confusing than
helpful.
- be consistent with function name spelling, particularly in the usage of
Caps.


-- 
Yours,

Andrei

=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.



More information about the Tutor mailing list