【发布时间】:2022-11-02 19:21:09
【问题描述】:
我想检查变量“类型”是否等于“视频”、“音频”或带有 while 循环的错误类型,但它不适用于 2 个条件。当我只输入 'while type != "video":' 它工作得很好,但是当我添加 'or type!= "audio":' 它停止工作你能帮我解决它吗?
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()
【问题讨论】:
-
作为程序员,您需要了解De Morgan''s laws
-
你的意思是:
while type != "video" and type!= "audio":? -
通常的英语语法并不总是适用于编程语言。我们无法区分英语中的“或”和“异或”,但这就是你想要的。 (德语也一样)因为我们也没有
xor关键字,所以我们最终使用and和not。 -
更好的是:
while type not in {"video", "audio"}:。但是你真的不应该将你的变量命名为type,因为你现在覆盖了内置的type。
标签: python while-loop