Lambda float | Sololearn: Learn to code for FREE!


Lambda float

How can I avoid to write tree times float before x? Os where another way? #task: 7% tax if p <= 20 else tax free #with lambda Code: p= ['2','10','80','99'] thats the input res3 = list(map(lambda x: float(x)*1.07 if float(x) <= 20 else float(x),p)) print(sum(res3))

7/23/2021 9:59:24 AM


4 Answers

New Answer


Angela o, those are strings. Didn't notice that. This should work: p= ['2','10','80','99'] res3 = list(map(lambda x: x*1.07 if x <= 20 else x,[float(i) for i in p])) print(sum(res3))


Logical error : 7% is 0.07 not 1.07 While convert everything to float? Inasmuch as you are multiplying by a float, you will end up with a float


NotAPythonNinja okay. I thought it was only the sum of the tax


res4 = list(map(lambda x: x *1.07 if x <= 20 else x,p)) File "", line 13, in <lambda> res4 = list(map(lambda x: x *1.07 if x <= 20 else x,p)) TypeError: '<=' not supported between instances of 'str' and 'int' I get these errors without float before. Please look again, because the input is in another format. W/o float it doesnt work.