How do I stop my else statement from running after my music plays?

0
11
Asked By CuriousCoder42 On

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!

3 Answers

Answered By PythonBro88 On

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!

Answered By TechyTina99 On

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.

Answered By CodeBuster55 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.