Skip to content
Advertisement

ValueError: could not convert string to float when adding percentages

I’d like to change value in a dictionary which use string as value. Here is what I did:

JavaScript

I got this error:

JavaScript

What I expect is ‘padding-left’ becomes 12%+0.4=52%

JavaScript

How can I accomplish this?

Advertisement

Answer

You can convert the digits to an integer and perform the addition:

JavaScript

This outputs:

JavaScript
Advertisement