'>' not supported between instances of 'str' and 'float'

203 Views Asked by At

I am if using if statement with or condition but this is showing error first I was wrong with none and or condiotns

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)
                        

The error is

2.0
None
1.0
'>' not supported between instances of 'str' and 'float'

1

There are 1 best solutions below

2
On

None or 0 will return 0. Indeed, x or y returns x if the truthiness of x is True and y otherwise.

You can work with:

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

or shorter:

if batch in (None, 0):
    # …

Furthermore your current_acerage is a string, not a float, so you can make a comparison with:

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
    )