1

I've been trying to run this Python code:

type = input("Please enter the file type ('video' or 'audio'): ")

while type != "video" or type != "audio":
    type = input("Please enter a valid format ('video' or 'audio'): ")
    if type == "video" or type == "audio":
        break

The problem: Even when the conditions aren't met (e.g: the format is 'video') the while loop begins, you can break out of it by simply typing the required format again, but it's very inconvenient, any help?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
santi
  • 29
  • 3
  • 2
    type would be "audio" or "video" so your statement will always be true cause if `type!="video"` is `False` (means that type is "video") then `type!="audio"` will be `True` ... so your condition will be always `True` – Ulises Bussi Nov 04 '21 at 20:32
  • 1
    BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Nov 04 '21 at 20:37
  • 1
    Related: [Asking the user for input until they give a valid response](/q/23294658/4518341) – wjandrea Nov 04 '21 at 20:39

5 Answers5

2

I think the correct code is:

t = ""

while t not in ["video", "audio"]:
    t = input("Please enter a valid format ('video' or 'audio'): ")
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
Adrian B
  • 1,490
  • 1
  • 19
  • 31
2
while type != "video" or type != "audio"

That while condition will ALWAYS be true.

If they entered "video", then the != "audio" part will be true.

If they entered "audio", then the != "video" part will be true.

If they entered anything else, then both parts will be true.

Use and instead of or:

while type != "video" and type != "audio"

Or, even better, check for membership in a list:

while type not in ["video", "audio"]:

And better still, don't use built-in names such as type for your variables.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
1

Change or by and:

# HERE -------------v
while t != "video" and t != "audio":
    print(t)
    t = input("Please enter a valid format ('video' or 'audio'): ")
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

NOT x OR NOT y is not the opposite of x OR y, that's NOT (x OR y). Or, using De Morgan's laws, NOT x AND NOT y.

That said, using not in is much simpler. See Adrian B's answer.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

I would put this logic in its own function to avoid complicated loop structures and make the code more readable. I would do something like this:

def get_media_type_input():
    while True:
        media_type = input("Please enter a valid format ('video' or 'audio'): ")
        if media_type in ("video", "audio"):
            return media_type
        else:
            print(f"Invalid media type {media_type}. Try again")

...

media_type = get_media_type_input()

You could then extend this to make the method more generic:

def get_format_type_input(title, choices):
    while True:
        choice = input(
            f"Please enter a valid {title} format "
            f"({', '.join(choices)}): "
        )
        if choice in choices:
            return choice
        else:
            print(f"Invalid {title} format '{choice}'. Try again.")


media_type = get_format_type_input("media", ["video", "audio"])
flakes
  • 21,558
  • 8
  • 41
  • 88