How to make a while loop with 2 conditions?

Question:

I want to check if the variable "type" equal "video", "audio" or a wrong type with a while loop but it isn’t working with 2 conditions. When I put only ‘while type != "video":’ it works perfectly but when I add the ‘or type!= "audio":’ it stops to work can you help me to fix it please?

type = input("Do you want a video or an audio? (answer by video or audio) n >> ")
while type != "video" or type!= "audio":
    print('Error! select an existing type')
    type = input("Do you want a video or an audio? (answer by video or audio) n >> ")
if type == "video":
    video_dwld()
elif type == "audio":
    audio_dwld()
Asked By: leoprosy

||

Answers:

As @quamrana commented, almost, it seems that you’re looping while type is not equal to one of the supported values.

It is always not equal to one of the supported values, since it’s at most equal to a single one of them.

Hint: use the power of Python:

supported_types = ("audio", "video")
while media_type not in supported_types:
    media_type = input(...)

Another hint: it’s easier to ask for forgiveness than for permission: use a dict to structure the handlers, and use try/except to deal with errors.


handlers = {
    "audio": audio_dwld,
    "video": video_dwld
}


while True:
    try:
        media_type = input(...)
        handle = handlers[media_type]
        handle()
    except KeyError:
        print("a useful error message") 
    else:
        break
Answered By: xtofl
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.