0

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)) https://code.sololearn.com/cX8VK5NB05ZI/?ref=app

23rd Jul 2021, 9:59 AM
Angela
Angela - avatar
4 Answers
+ 3
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))
23rd Jul 2021, 11:06 AM
Baribor Saturday
Baribor Saturday - avatar
0
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
23rd Jul 2021, 10:09 AM
Baribor Saturday
Baribor Saturday - avatar
0
NotAPythonNinja okay. I thought it was only the sum of the tax
23rd Jul 2021, 10:40 AM
Baribor Saturday
Baribor Saturday - avatar
0
res4 = list(map(lambda x: x *1.07 if x <= 20 else x,p)) File "file0.py", 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.
23rd Jul 2021, 10:55 AM
Angela
Angela - avatar