我是 if 使用带有 or 条件的 if 语句 但这显示错误 首先我错了没有和或条件
print(farm_acerage)
print(batch_acerage)
print(current_acerage)
if farm_acerage in (None, 0):
return Response({"error": True, "message": "Add farm first"},
status = status.HTTP_200_OK)
if farm_acerage is not None and batch_acerage in (None, 0):
if current_acerage > farm_acerage:
return Response({"error": True, "message": "Ckkannot add batch more than farm capacity"},
status = status.HTTP_200_OK)
if farm_acerage is not None and batch_acerage is not None:
batch_acerage = float(batch_acerage) + float(current_acerage)
if batch_acerage > farm_acerage:
return Response({"error": True, "message": "Cannot add batch more than farm capacity"},
status=status.HTTP_200_OK)
错误是
2.0
None
1.0
'>' not supported between instances of 'str' and 'float'
最佳答案
None or 0
将返回0
。确实,<em>x</em> or <em>y</em>
返回 x
如果 x
的真实性 是 True
和 y
否则。
您可以使用:
if batch <strong>== None</strong> or batch <strong>== 0</strong>:
# …
或更短:
if batch <strong>in</strong> (None, 0):
# …
此外,您的current_acerage
是 str
ing,不是float
,因此您可以与以下内容进行比较:
if batch_acerage in (None, 0) and <strong>float(</strong>current_acerage<strong>)</strong> > farm_acerage:
return Response(
{"error": True, "message": "Ckkannot add batch more than farm capacity"},
status = status.HTTP_200_OK
)
关于django - '>' not supported between instances of ' str'和 'float',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71083492/