interactive programme (voice)
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue May 30 20:48:32 EDT 2006
nigel a écrit :
> hi i have wrote an interactive programme,this is a small section of it.
> #This is my first programme writing in python
> s = raw_input ("hello what's your name? ")
> if s=='carmel':
> print "Ahh the boss's wife"
> if s=='melvyn':
> print "your the boss's dad"
> if s=='rebecca':
> print "you must be the wreath woman"
> if s=='gareth ':
> print "You must be the trucker"
> if s=='carol':
> print "you must be my boss's mom"
The problem with this code is that:
- it does a lots of useless tests (ie : even if the user enters
'carmel', all other names will be tested too).
- it doesn't handle the default case (any other name than the one you
test for)
A minimal amelioration would be to use if/elif/else:
s = raw_input ("hello what's your name? ")
if s == 'carmel':
print "Ahh the boss's wife"
elif s == 'melvyn':
print "your the boss's dad"
elif s == 'rebecca':
print "you must be the wreath woman"
elif s == 'gareth ':
print "You must be the trucker"
elif s=='carol':
print "you must be my boss's mom"
else: # default
print "I'm afraid I don't know you..."
Now this is a little better, but still not very pythonic. We have a nice
thing in Python named a dict (for 'dictionnary'). It stores pairs of
key:value - and FWIW, it's the central data structure in Python, so
you'll see them quite a lot. There very handy for this kind of use case:
greetings = {
'carmel' : "Ahh the boss's wife",
'melvyn' : "your the boss's dad",
'rebecca': "you must be the wreath woman",
'gareth ': "You must be the trucker",
'carol' : "you must be my boss's mom",
}
name = raw_input ("hello what's your name? ")
print greetings.get(name, "I'm afraid I don't know you...")
> What i was wandering is there a way i can get sound,
<sorry>
Now this is a sound question !-)
</sorry>
>i mean instead of getting
> it to just print text on my screen i would like my computer to say it.
This depends mostly on your computer and what's installed on it. And
this is not part of the standard lib AFAIK.
More information about the Python-list
mailing list