0

I am currently writing my first program (YT Downloader), which includes collecting user decisions on certain actions. Currently, I check for user decisions using formulas like this:

while True:
    downloadCheck = input("\nDo you want to download this video? (y/n) ")
    if downloadCheck == "y":
        download()
        break
    elif downloadCheck == "n":
        print("No download triggered. Program will exit")
        break
    else:
        print(r'Please answer using "y" for yes or "n" for no')
        continue

I do use this while true --> if/elif/else combination quite a lot of times in the code and was wondering if there is a simpler / more elegant / more professional way to check the user input.

Thankful for recommendations!

  • continue isn't necessary, other than that this way is perfectly acceptable. If there is really significant amount of such statements, you can move it to a function that accepts some kind of dictionary mapping valid input to some actions and default action in case input is not valid. – matszwecja Feb 24 '22 at 15:55
  • Write a function that takes the question and the reject message as arguments, and returns a boolean. But the true professional way requires the code to be case insensitive. –  Feb 24 '22 at 15:57

0 Answers0