How about doing it this way:
def is_yes(question):
yn = { 'y':True, 'yes':True, 'n':False, 'no':False }
while True:
try:
return yn[raw_input(question).lower().strip()]
except KeyError:
print '\nplease select y, n, yes, or no\n'
___
tring. The OP didn't have that limitation to
deal with.
Alan G.
>
>
> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf
> Of Alan Gauld
> Sent: Thursday, June 07, 2007 1:45 AM
> To: tutor@python.org
> Subject: Re: [Tutor
program was: i++
On Wed, 6 Jun 2007, David Heiser wrote:
> or..
>
> def is_yes(question):
> while True:
> try:
> s = raw_input(question).lower()[0]
> if s == 'y':
> return True
> elif s =
What if the user enters "maybe".
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf
Of Alan Gauld
Sent: Thursday, June 07, 2007 1:45 AM
To: tutor@python.org
Subject: Re: [Tutor] Engarde program was: i++
"David Heiser" <[EMAIL PROT
On Wed, 6 Jun 2007, David Heiser wrote:
> or..
>
> def is_yes(question):
> while True:
> try:
> s = raw_input(question).lower()[0]
> if s == 'y':
> return True
> elif s == 'n':
> return False
> except:
>
"David Heiser" <[EMAIL PROTECTED]> wrote
>
> def is_yes(question):
> while True:
> try:
> s = raw_input(question).lower()[0]
> if s == 'y':
> return True
> elif s == 'n':
> return False
> except:
>
he condition where a user just
presses enter
print '\nplease select y, n, yes, or no\n'
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of scott
Sent: Wednesday, June 06, 2007 3:08 PM
To: tutor@python.org
Subject: Re: [Tutor] Engarde progr
Danny Yoo wrote:
> Double check the definition of mfile_read(). It has a possible type
> error in terms of the values it returns back to the user.
Oh, I see. I forgot to change the "if file does not exist part" when i
changed the other half. I have changed it and is seems to work when
there i
Alan Gauld wrote:
> You never change i so this is always true.
> Therefore you can express that better with...
Thanks for your suggestions, I put together the following based on them:
##
def is_yes(question):
while True:
s = raw_input(questi
On Wed, 6 Jun 2007, scott wrote:
> Danny Yoo wrote:
>> In the context of the new Master_stats class, it makes more sense for
>> mfile_read() to return a Master_stats object now rather than a two-tuple
>> list.
> I was able to change the [year, month] to just master and it worked fine. I
> w
"scott" <[EMAIL PROTECTED]> wrote
> added one in. I assume that it is good programming practise to
> close
> all open files when they are not needed anymore?
Yes, its good practice.
> I wrote the is_yes as follows and it seems to work fine.
Almost, but not quite ideal, see below:
>
Danny Yoo wrote:
In the context of the new Master_stats class, it makes more sense for
mfile_read() to return a Master_stats object now rather than a two-tuple
list.
I was able to change the [year, month] to just master and it worked
fine. I was also able to do master = mfile_read() and it wor
> There where some values I did not want saved to the character file. A couple
> where values that are for all characters, so I put them into their own class.
>
> ###
> class Master_stats:
> def __init__(self, year, month):
> self.year = year
> self
Danny Yoo wrote:
I will work at your suggestions and will get back to you if I have any
problems.
Good! Keep the folks on Python-Tutor up to date with your progress.
Here is the code changes I made based on your suggestions:
I put all the values together into a class
###
14 matches
Mail list logo