0
Help in challenge â Python
Once again, I can not access dictionary items. The challenge is called: Ballpark orders. Code as follows: menu = { "Nachos" : 3.00, "Pizza" : 3.00, "Cheeseburger" : 10.00, "Water" : 4.00, "Coke" : 5.00} order = input() list_order = list(order.split(" ")) a = 0 for x in list_order: if x in menu: a = a + menu.get(x) if x not in menu: a = a + menu.get("Coke") print(float(a*1.07)) Any guidance?
7 Answers
+ 8
list(order.split(" ")) not needed since .split(" ") turns it already into a LIST
order variable is not needed to be a variable since you never use it except to make a new variable by using a method of strings.
for dictitionaries you can use .get and since if the x in list_order is not in the list it will always result in Coke which is always 5.0 you can shorten it to menu.get(x, 5)
also using float() when you multiply by 1.07 with a already makes a float since double is NOT a datatype of python unlike other languages such as C, C++, java etc.
I am not sure why you cannot access it but you can shorten your code to this
menu = {
"Nachos" : 3.00,
"Pizza" : 3.00,
"Cheeseburger" : 10.00,
"Water" : 4.00
}
a = 0
list_order = input().split()
for x in list_order:
a += menu.get(x, 5)
# remember to round since it says that in the problem
print(round(a * 1.07, 2)
Also remember to use round() in the future!
+ 3
I see how it is now. Lwez and Isak Nilsson thanks for guidance and further explanation!
+ 2
Your mistake :
Nachos cost 6$
Pizza cost 6$
And you should round the sum
print(round(a*1.07,2))
+ 2
Also what @Lwez said
"Your mistake :
Nachos cost 6$
Pizza cost 6quot;
+ 2
Lamron np good luck! :D
+ 2
Keep pushing yourself to be better and never stop learning
+ 2
I'll not đ
Just need more practice to form better implementation.
I see what I did wrong, and why it was wrong, therefore the problem was solved. Thanks guys