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?

26th Dec 2022, 9:24 PM
Lamron
Lamron - avatar
7 Antworten
+ 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!
26th Dec 2022, 9:52 PM
Vanessa Nilsson
+ 3
I see how it is now. Lwez and Isak Nilsson thanks for guidance and further explanation!
26th Dec 2022, 9:56 PM
Lamron
Lamron - avatar
+ 2
Your mistake : Nachos cost 6$ Pizza cost 6$ And you should round the sum print(round(a*1.07,2))
26th Dec 2022, 9:50 PM
Lwez
Lwez - avatar
+ 2
Also what @Lwez said "Your mistake : Nachos cost 6$ Pizza cost 6
quot;
26th Dec 2022, 9:53 PM
Vanessa Nilsson
+ 2
Lamron np good luck! :D
26th Dec 2022, 9:56 PM
Vanessa Nilsson
+ 2
Keep pushing yourself to be better and never stop learning
26th Dec 2022, 10:02 PM
Lwez
Lwez - avatar
+ 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
26th Dec 2022, 10:09 PM
Lamron
Lamron - avatar