+ 1
Lambdas - Python Developer
I am working through the Python Developer course and am currently on the section about lambdas. I attempted this code coach but keep failing a hidden test case. What am I doing wrong? https://code.sololearn.com/cfltk1CJCmxL/?ref=app
4 Antworten
+ 7
Lashana Jegatheswaran ,
the code and the formula you have applied is correct, but it can create an unexcepted output.
> x/100*y the reason is that you first divide x by 100, then multiply it by y.
> x*y/100 this version will work
> the reason for this is the float precision issue.
# your version:
x = 69
y = 13
result = 8.969999999999999
# suggested version:
x = 69
y = 13
result = 8.97
the preferred version is:
...
res = (lambda x,y:x*y/100)(price, perc)
...
+ 3
Hmm..
Try changing the order of x and y to this..
https://code.sololearn.com/cqLQhJCkN63t/?ref=app
I tried your code order with input 10 and 3 for price and perc, respectively and it gives me 0.300000...4, while my order gives 0.3. This is because the computer have its own "definition" for floating point numbers. It has its own way to perform operations with floating point numbers, so yeah..
If you are interested in, you can search up for it.
+ 1
Thank you all so much!
0
These are the instructions for the code:
You are given code that should calculate the corresponding percentage of a price.
Somebody wrote a lambda function to accomplish that, however the lambda is wrong.
Fix the code to output the given percentage of the price.
Sample Input
50
10
Sample Output
5.0