+ 4
Ballpark order challenge
I have been trying to solve Code Coach "Ballpark order challenge" which can be viewed at this link-- https://www.sololearn.com/coach/15?ref=app I have written a solution which fails all three hidden tests but gives desired output for the first two. I am unable to find the bug in my code. Can you please help me?? Here is my code-- menu = { "Nachos":6, "Pizza" :6, "Cheeseburger":10, "Water":4, "Coke":5 } order = input().split(" ") price = 0 for item in order: price += menu.get(item,5) #final price is price of all items ordered + tax final_price = price * 1.07 print (final_price)
14 odpowiedzi
+ 2
CHANDAN ROY
Solved
---------
menu = {
"Nachos":6.00,
"Pizza" :6.00,
"Cheeseburger":10.00,
"Water":4.00,
"Coke":5.00
}
order = input().split(" ")
price = 0.00
for item in order:
if item in menu:
price += menu[item]
else:
price += menu['Coke']
#print (price)
#final price is price of all items ordered + tax
final_price = price + (price * 7) / 100
print (final_price)
---------
Read this task again:
"Determine the total cost of ordering four items from the concession stand. If one of your friend’s orders something that isn't on the menu, you will order a Coke for them instead."
+ 5
CHANDAN ROY
menu.get will also work. You can try.
But in case of price * 1.07 you may get 10.0000003 or something else.
+ 2
I Am Groot ! Sir, can you elaborate your last point a bit ??
Why is final_price = price *1.07 different from final_price = price *107/100
+ 2
CHANDAN ROY I don't know the actual reason but when I tried with dummy value then I got something like that.
+ 1
I Am Groot ! Sir, I just found out the mistake I was making.
when I put
final_price = price *1.07
#it fails the test
but when I change final_price to
final_price = price *107/100 or the way you have written it.
it works.
can you explain why it happens???
+ 1
A short explanation:
Its about how floats are stored by programming languages (binary), which cause to sometimes give funny results like 17.1000000000000000001.
To work around it, you can use up the round() function, whereas first argument is the number or operation, and the second one, the number of decimals (by default is 0)
+ 1
My code:
x = input().split(" ")
menu = {"Pizza"}
sum = 0
for i in x:
if i=="Pizza":
sum +=6
elif i=="Nachos":
sum +=6
elif i=="Cheeseburger":
sum+=10
elif i=="Water":
sum+=4
else:
sum+=5
fsum = sum + (sum*7)/100
print (fsum)
0
Abol
Do you suggest the following changes in for loop??
for item in order:
if item in menu:
price+= menu[item]
else:
price+=4
I tried this and it produced the same result. It passes first two tests but fails the rest hidden ones.
I don't think using different methods of same type can solve an issue.
There is some error in my code. Help me trace that brother so that I can find peace 😄
0
Abol question is in the link given.
Click on this click and you will get the question. https://www.sololearn.com/coach/15?ref=app
0
I Am Groot !
JOY
Sir help