你如何在python 3.x中使用或运算?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你如何在python 3.x中使用或运算?相关的知识,希望对你有一定的参考价值。
def members_ID():
meme = False
membersID = str(input("enter members ID"))
while (meme) == False:
meme = True
if len(membersID) == 4:
if not (membersID)[0] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[1] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[2] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
if not (membersID)[3] == "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9":
meme = False
else:
meme = False
print("members ID must be in the format 1111")
membersID = str(input("enter members ID"))
if meme == True:
print(membersID)
elif (meme) == False:
print("members ID must be in the format 1111")
如果它不是四个它可以工作,但如果是,那么程序将停止而不工作?
它可能与or
运营商有关,因为当我拿出它们并输入4位数字所需的信息然后它可以工作。
def members_ID():
meme = False
membersID = str(input("enter members ID"))
while (meme) == False:
meme = True
if len(membersID) == 4:
if not (membersID)[0] == "0":
meme = False
if not (membersID)[1] == "0":
meme = False
if not (membersID)[2] == "0":
meme = False
if not (membersID)[3] == "0":
meme = False
else:
meme = False
print("members ID must be in the format 1111")
membersID = str(input("enter members ID"))
if meme == True:
print(membersID)
elif (meme) == False:
print("members ID must be in the format 1111")
但是如果输入的信息是错误的。例如,ssss
将使程序停止工作,但0000
将(如果0000
是所需的信息)!
你不能以这种方式使用or
。下列:
not (membersID)[0] == "0" or "1"
是true
,无论是not membersID[0] == "0"
还是字符串"1"
都是非空的,它始终是。因此,if
声明始终是true
。
你的意图应该写成:
not (membersID[0] == "0" or membersID[0] == "1" or membersID[0] == "2") # etc. for 3..9
为了避免重复这么多,你可以这样做:
membersID[0] not in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
or
-operator位于两个语句或子句之间,并返回非None,False或等效的第一部分(如空字符串,列表等)
看这个例子:
a = 'a'
b = 'b'
print(a == 'b' or 'a')
# 'a'
print(a == 'a' or 'b')
# True
首先,它打印出值'a',因为a == 'b'
为False,'a'不为空,False或None。
其次,它打印出值True
,因为a == 'a'
为True。
如果您希望值为None,则这很方便:
x = some_variable or 5
这里,当x
为None,False(或空)时,some_variable
为5。
所以在你的链中:
如果不是(membersID)[0] ==“0”或“1”或“2”或“3”或“4”或“5”或“6”或“7”或“8”或“9”: meme =假
我希望meme = False
可以在任何情况下运行,因为如果你将它重新标记为:
a = not (membersID)[0] == "0"
b = "1"
c = "2"
#...
然后你的if语句就是
if a or b or c # or d ...
meme = False
只有第一个条款,a
将有机会不成真。
换句话说,如果if something
具有价值,那么做something
将是真的:
if "2":
print('This is a tautology')
此外,如果or
s链中的任何子句都没有被解析为True,它将返回最后一个子句:
x = None or False or None
print(x is None)
# True
print(type(x))
# NoneType
x = None or False
print(x)
# False
这是我根据Thijs van dien的答案完成的答案。 (谢谢)
def members_ID():
meme = False
membersID = input("enter members ID")
while (meme) == False:
meme = True
if len(membersID) == 4:
if membersID[0] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[1] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[2] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if membersID[3] not in ["0","1","2","3","4","5","6","7","8","9"]:
meme = False
if meme == True:
print(membersID)
elif meme == False:
print("enter in the format 1111")
membersID = input("enter members ID")
else:
meme = False
print("members ID must be in the format 1111")
membersID = input("enter members ID")
membersID = str(input("enter members ID"))
input()已经返回一个字符串。在此行中,您尝试在输入字符串时将字符串转换为字符串。因此,它会引发错误。纠正它:
membersID = input("enter members ID")
以上是关于你如何在python 3.x中使用或运算?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 python 3.x tkinter 中保存文本框(字符串和整数)