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
7/23/2021 9:59:24 AM
Angela
4 Answers
New AnswerAngela 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
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.