interactive programme (voice)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nigel

    #1

    interactive programme (voice)

    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

  • Bruno Desthuilliers

    #2
    Re: interactive programme (voice)

    nigel a écrit :[color=blue]
    > 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"[/color]

    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(n ame, "I'm afraid I don't know you...")
    [color=blue]
    > What i was wandering is there a way i can get sound,[/color]

    <sorry>
    Now this is a sound question !-)
    </sorry>
    [color=blue]
    >i mean instead of getting
    > it to just print text on my screen i would like my computer to say it.[/color]

    This depends mostly on your computer and what's installed on it. And
    this is not part of the standard lib AFAIK.

    Comment

    Working...