-2

所以现在我有

if request.POST['superPoints'].count('.') == False:

然后它继续到其他代码。但是如果我添加

if request.POST['cashTexts'].count('.') and request.POST['superPoints']('.') == False:

它总是转到 else: 语句,无论在两种形式中输入什么。因此,当您尝试计算两件事时,是否会出现其他不是 False 的东西并扰乱流程?为什么结合这些语句不起作用?

编辑:
如果我这样做,它会起作用:

if request.POST['cashTexts'].count('.') == False:

所以我不认为这是其中一个领域的问题。

编辑:: 如果我将它们设置为!= True,它会起作用。不知道为什么,但我一定得到了一些错误以外的东西。

4

2 回答 2

2

尝试:

if not request.POST['cashTexts'].count('.') and not request.POST['superPoints']:

我相信您的问题与运算符优先级有关。

你在做:

test1 and test2 == False

这转化为:

test1 and (test2 == False)

这与以下内容相同:

test1 == True and test2 == False
于 2012-04-16T13:27:15.630 回答
2

在 Python 中测试真实性的首选方法是:

if obj:
    pass

代替:

if obj == True:
    pass

同样对于虚假:

if not obj:
    pass

代替:

if obj == False:
    pass

此外,.count()字符串方法返回子字符串的出现次数。如果您只想测试一个字符是否在字符串中至少出现一次,请使用以下命令:

if '.' in mystr:
    pass

如果要测试字符是否不在字符串中,请使用以下命令:

if '.' not in mystr:
    pass

因此,如果您想测试任一字段中是否没有点,请执行以下操作:

if '.' not in request.POST['cashTexts'] and '.' not in request.POST['superPoints']:
    pass
于 2012-04-16T13:50:55.027 回答