How do I stop my else statement from running after my music plays?
Asked by
CuriousCoder42
I'm trying to create a simple program in Python that lets users choose between two songs. I have a loop that keeps asking for input until the user types "back". The problem is, after the beeping sequence for the selected song, my else statement runs, giving an unexpected output. How can I modify my code to prevent the else statement from executing after the music plays? Thanks for your help!
Answers
Yep! And remember, right after you handle a valid input, using a `continue` statement can also help in managing the control flow, which keeps it from executing further unnecessary checks.
Reply to this answer
Your issue is that the else statement is linked to the last if condition. After the beeping plays, the program checks if `scelta2` is 'back', which it's not, so the else runs. To fix this, consider using `elif` for your song choices and keep the else for handling invalid inputs. That way, if a song plays, the program won't hit the else condition unless no valid input was given.
Reply to this answer
Exactly, what you want is to structure it with `if`, `elif`, and then `else`. After you play the music, it still goes through the last check with else. By structuring it correctly, you can avoid this issue entirely!
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions