ValueError at /cart could not convert string to float: ”

I am new here and currently trying to make an add-to-cart page in my project but I am receiving this error as ValueError: could not convert string to float: “

It says that my error is coming from my views.py file line 128, so this is my line 128:

# Cart List Page
def cart_list(request):
    total_amt=0
    if 'cartdata' in request.session:
        for p_id,item in request.session['cartdata'].items():
            total_amt+=int(item['qty'])*float(item['price'])
        return render(request, 'cart.html',{'cart_data':request.session['cartdata'],'totalitems':len(request.session['cartdata']),'total_amt':total_amt})
    else:
        return render(request, 'cart.html',{'cart_data':'','totalitems':0,'total_amt':total_amt})

EDIT: I have looked up for solutions here but none worked for me and I did check there is no spacing in between either

When I refresh my page , I am getting an error like this:

ValueError at /cart
could not convert string to float: ''

Answer

item['price'] contains an empty string. You should check if item['price'] is a valid number:

def check_num(num_type, value):
    try:
        return num_type(value)
    except ValueError:
        return None

for p_id, item in request.session['cartdata'].items():
    price = check_num(float, item['price'])
    if price is None:
        # Do something if price is not a valid number
        # e.g. set it to a default value, such as zero
        ...
  
    qty = check_num(int, item['qty'])
    if qty is None:
        # Do something if qty is not a valid number
        # e.g. set it to a default value, such as zero
        ...
    
    total_amt += qty * price