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)
10 Antworten
+ 7
res = (lamba x, y: x*y/100)(price, perc)
+ 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))
+ 3
This code doesn't use lambda but it works
price = int(input())
perc = int(input())
print((perc/100)*price)
+ 1
Thanks visph !
+ 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.
+ 1
visph Thank you very much for your explanation . I'll try to dig in more in that topic
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)
0
price = int(input())
perc = int(input())
res = (lambda x,y:x*y/100)(price, perc)
print(res)
0
My solution:
price = int(input())
perc = int(input())
res = (lambda x,y:x*y/100)(price, perc)
print(res)
0
price = int(input())
perc = int(input())
res = (lambda x,y:x*y/100)(price, perc)
print(res)