2
from playsound import playsound
notes = input("What would you like to play?:\n").split(", ")


for c in notes:
    if "a" or "A" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/a5.mp3')
    else:
        pass
    if "b" or "B" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/b5.mp3')
    else:
        pass
    if "c" or "C" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/c5.mp3')
    else:
        pass
    if "d" or "D" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/d5.mp3')
    else:
        pass
    if "e" or "E" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/e5.mp3')
    else:
        pass
    if "f" or "F" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/f5.mp3')
    else:
        pass
    if "g" or "G" in notes:
        playsound('/Users/Keller/Downloads/mp3 Notes/g5.mp3')
    else:
        pass

Please excuse the gross cluster of if statements. My intention was to fix it once I got the code up and running. I realize now this is probably part of the problem.

Kiri.
  • 41
  • 4
  • 1
    You should use a dictionary instead of multiple if elses and simply grep for the letter in dict keys. – NelsonGon May 22 '22 at 23:57
  • What input did you pass into notes, what did it play (which notes in what order), and what did you expect it to play? – M. Chak May 22 '22 at 23:57
  • 1
    @NelsonGon Thanks for the feedback. New-ish to Python, so I appreciate it. I'll look into it. – Kiri. May 22 '22 at 23:58
  • 3
    `"a" in notes or "A" in notes`. Currently you're doing `if "a"` which is always `True`. – Loocid May 22 '22 at 23:58
  • @M.Chak I just input a, or any other letter, and it's supposed to play only the audio file for that note. Instead, whenever I input any note it loops through and plays the audio file for every note. – Kiri. May 22 '22 at 23:59
  • As @Loocid noted, all of your conditions are always true. – Unrelated String May 23 '22 at 00:00
  • 1
    A dict is honestly overkill. The only part of the filename that changes is the note, so use an f-string: `playsound(f'/Users/Keller/Downloads/mp3 Notes/{note}5.mp3')` – ddejohn May 23 '22 at 00:05
  • should be `if "a" in c or "A" in c` instead – MiH May 23 '22 at 00:09
  • 1
    Instead of all the `if`s, just do one line: `playsound(f'/Users/Keller/Downloads/mp3 Notes/{c.lower()}5.mp3')`. Wrap it in a `try` to catch whatever exception it raises if there's no such file. – Samwise May 23 '22 at 00:11

0 Answers0