即使不满足参数,While 循环也会开始
While loop is starting even when parameters aren't met
我一直在尝试 运行 这个 Python 代码:
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
问题:即使不满足条件(例如:格式为 'video'),while 循环开始时,您可以通过再次键入所需的格式来跳出它,但这非常不方便,有什么帮助吗?
我认为正确的代码是:
t = ""
while t not in ["video", "audio"]:
t = input("Please enter a valid format ('video' or 'audio'): ")
将or
更改为and
:
# HERE -------------v
while t != "video" and t != "audio":
print(t)
t = input("Please enter a valid format ('video' or 'audio'): ")
while type != "video" or type != "audio"
while 条件永远为真。
如果他们输入“视频”,则 != "audio"
部分将为真。
如果他们输入“音频”,则 != "video"
部分为真。
如果他们输入了其他任何内容,则两部分都为真。
使用and
代替or
:
while type != "video" and type != "audio"
或者,更好的是,检查列表中的成员资格:
while type not in ["video", "audio"]:
更好的是,不要为变量使用 type
等内置名称。
NOT x OR NOT y
不是x OR y
的反义词,而是NOT (x OR y)
。或者,使用德摩根定律,NOT x AND NOT y
.
也就是说,使用 not in
要简单得多。参见 。
我会把这个逻辑放在它自己的函数中,以避免复杂的循环结构并使代码更具可读性。我会做这样的事情:
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()
然后您可以扩展它以使该方法更通用:
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"])
我一直在尝试 运行 这个 Python 代码:
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
问题:即使不满足条件(例如:格式为 'video'),while 循环开始时,您可以通过再次键入所需的格式来跳出它,但这非常不方便,有什么帮助吗?
我认为正确的代码是:
t = ""
while t not in ["video", "audio"]:
t = input("Please enter a valid format ('video' or 'audio'): ")
将or
更改为and
:
# HERE -------------v
while t != "video" and t != "audio":
print(t)
t = input("Please enter a valid format ('video' or 'audio'): ")
while type != "video" or type != "audio"
while 条件永远为真。
如果他们输入“视频”,则 != "audio"
部分将为真。
如果他们输入“音频”,则 != "video"
部分为真。
如果他们输入了其他任何内容,则两部分都为真。
使用and
代替or
:
while type != "video" and type != "audio"
或者,更好的是,检查列表中的成员资格:
while type not in ["video", "audio"]:
更好的是,不要为变量使用 type
等内置名称。
NOT x OR NOT y
不是x OR y
的反义词,而是NOT (x OR y)
。或者,使用德摩根定律,NOT x AND NOT y
.
也就是说,使用 not in
要简单得多。参见
我会把这个逻辑放在它自己的函数中,以避免复杂的循环结构并使代码更具可读性。我会做这样的事情:
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()
然后您可以扩展它以使该方法更通用:
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"])