0

Intermediate python - 12.2 ‘How Much’ - Practice help

Not sure what’s wrong and tried with no luck. Please help, thanks! 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 Current code: price = int(input()) perc = int(input()) res = (lambda x,y:x-y)(price, perc) print(res)

3rd Jul 2021, 5:37 PM
Zach Z
10 Antworten
+ 7
res = (lamba x, y: x*y/100)(price, perc)
3rd Jul 2021, 5:44 PM
visph
visph - avatar
+ 3
""" Kazantsev Alexander: that's a question of calcul precision... Mathematically, both should compute the same result, but in computer, there's little difference sometimes, due to the finite precision of floating number storage. To find other example where both are NOT equals, run the below script: at each run, it will generate random values in a loop while both results are equals, then break ans output the numbers wich cause differents results (and the two "almost" equals result) ^^ """ from random import randint as rnd f1 = lambda x, y: x*y/100 f2 = lambda x, y: (x/100)*y while True: p = rnd(1,100)/rnd(1,100) r = rnd(1,100)/rnd(1,100) if f1(p,r)!=f2(p,r): break print(p,r) print(f1(p,r)) print(f2(p,r))
17th Apr 2022, 9:33 PM
visph
visph - avatar
+ 3
This code doesn't use lambda but it works price = int(input()) perc = int(input()) print((perc/100)*price)
28th Oct 2022, 8:08 AM
Dave Kendrick Turmawan
Dave Kendrick Turmawan - avatar
+ 1
Thanks visph !
4th Jul 2021, 6:11 PM
Zach Z
+ 1
I'm thinking the same way. Can someone show example of hidden test 4 or explain why doesn't this solution work [res = (lambda x,y: (x/100) * y) (price, perc)]. Could someone give another example when it is not solvable.
17th Apr 2022, 1:59 PM
Kazantsev Alexander
+ 1
visph Thank you very much for your explanation . I'll try to dig in more in that topic
18th Apr 2022, 3:00 PM
Kazantsev Alexander
0
Why do you think this answer is not suitable? Accepts all tests except one hidden price = int(input()) perc = int(input()) res = (lambda x,y: (x/100) * y) (price, perc) print(res)
22nd Jan 2022, 10:37 AM
Артём Моторин
Артём Моторин - avatar
0
price = int(input()) perc = int(input()) res = (lambda x,y:x*y/100)(price, perc) print(res)
10th May 2022, 3:43 PM
Stella
Stella - avatar
0
My solution: price = int(input()) perc = int(input()) res = (lambda x,y:x*y/100)(price, perc) print(res)
10th Nov 2022, 8:22 AM
Pooja Patel
Pooja Patel - avatar
0
price = int(input()) perc = int(input()) res = (lambda x,y:x*y/100)(price, perc) print(res)
23rd Feb 2023, 4:22 AM
SahanEra
SahanEra - avatar