2

我是如果使用带有或条件的 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'

4

1 回答 1

2

None or 0将返回0。确实,如果is的真实性返回,则返回。x or yxxTruey

您可以使用:

if batch == None or batch == 0:
   # …

或更短:

if batch in (None, 0):
    # …

此外,您的current_acerageis a string,而不是 a float,因此您可以与以下内容进行比较:

if batch_acerage in (None, 0) and float(current_acerage) > farm_acerage:
    return Response(
        {"error": True, "message": "Ckkannot add batch more than farm capacity"},
        status = status.HTTP_200_OK
    )
于 2022-02-11T16:37:48.977 回答