interactive programme (voice)

N

nigel

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"

What i was wandering is there a way i can get sound,i mean instead of getting
it to just print text on my screen i would like my computer to say it.
Thanks nige
 
B

Bruno Desthuilliers

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 !-)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top