“Ballpark orders” challenge troubles | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

“Ballpark orders” challenge troubles

Hi all, I’m trying to complete the Code Coach challenge “Ballpark Orders” using Python. I want the code to take an input string of several words (food orders), compare those to a dictionary, and add up the total price of each item that was ordered. This results in 5, so I assume something is wrong with my if statement; I don’t think it is checking the words in the string in the way that I want it to. Any help? 1 prices = { 2 "nachos": 6.0, 3 "pizza": 6.0, 4 "cheeseburger": 10.0, 5 "water": 4.0, 6 "coke": 5.0 7 } 8 9 order = input() #example: “water water coke pizza” 10 order = (x for x in order) 11 12 def place_order(dict, key): 13 total = 0 14 if key in dict: 15 total += dict.get(key) 16 return total 17 else: 18 total += 5.0 19 return total 20 21 result = place_order(prices, order) 22 23 print(result) EDIT I got as far as making the order into separate strings with this: order = "pizza pizza water coke" order = order.split() total= 0 if order in prices: total += prices.get(order) print(total) else: total += 5.0 print(total) Now the error I receive is that lists are unhashable. Hmm!

11th Apr 2020, 7:13 PM
Joel
3 Answers
+ 2
Justus thank you! I forgot that “if” statements aren’t loops. I know, that’s a silly mistake. I actually solved this using a while loop! Here’s where I got to: prices = { "nachos": 6.0, "pizza": 6.0, "cheeseburger": 10.0, "water": 4.0, "coke": 5.0 } order = input() order = str(order) order = order.lower() order_list = order.split() def place_order(dict, key): n=0 total=0 while n <4: if key[n] in dict: total += dict.get(key[n]) n += 1 else: total += 5.0 n += 1 total += total * .07 return total print(place_order(prices, order_list))
12th Apr 2020, 6:59 PM
Joel
+ 1
My account isn't pro, so, don't know if this will pass all test cases. Try using a for loop to iterate through the list . Something like this result =[] for i in order: if i in dictionary: result.append(dictionary[i]) print(sum(result))
11th Apr 2020, 7:58 PM
Justus
Justus - avatar
0
Drop the link to your code
11th Apr 2020, 7:58 PM
Justus
Justus - avatar