Play audio file when keyboard is pressed
<p>I have the following code in which I capture the user input and then I want to parse it and evaluate each character in string using ASCII code to play a certain .mp3 file:  </p>

<p>The problem is that this code works only for the first character. For example, if I have the input as <code>ab</code> I only hear the audio file for <code>a</code> and not <code>b</code></p>

<pre><code>import os 

wrd=raw_input("Please write something: ") 
wrd=(str(wrd)).lower() 
wrd=list(wrd) 
i=0 
print (wrd[0:len(wrd):1]) 

for wrd[i] in wrd:
    print wrd[i]
    if ord((wrd[i]))==97:
        os.system("start C:/Users/letters/a(1).mp3")
        i+=1
    if ord((wrd[i]))==98:
        os.system("start C:/Users//letters/b(1).mp3")
        i+=1
</code></pre>

<p>If the mp3 file name always follow the path "letter(1),mp3" you can do something like:</p>

<pre><code>import os

wrd = input("Please write something: ")
wrd = wrd.lower()

for char in wrd:
   try:
       os.system("start " + char +'(1).mp3')
   except:
       ValueError
</code></pre>

<p>otherwise you can use a dictionary and fill it with the right filename for each letter:</p>

<pre><code>db = {
   'a': 'C:/Users//letters/a(1).mp3',
   'b': 'C:/Users//letters/b(1).mp3'
   # etc
}
...
   try:
       os.system("start " + db[char])
...
</code></pre>

python os.system