0

CodeCoach Ballpark Order

Hi, can anyone help me with my code? The output is higher than it should. Thank you! https://code.sololearn.com/cIvt4g19WK15/?ref=app

1st Mar 2023, 10:44 PM
Camelia
Camelia - avatar
8 ответов
+ 2
I assume input is just one string so... order = input() list = {"Nachos":6, "Pizza":6, "Cheeseburger":10, "Water":4, "Coke":5} total = 0 for i in order.split(): if i in list: total += list[i] else: total += list ["Coke"] sum = total + ((total*7)/100) print(sum) ....Bad practice using python built-in names eg sum and list.
1st Mar 2023, 11:13 PM
rodwynnejones
rodwynnejones - avatar
+ 2
order = input() list = {"Nachos":6, "Pizza":6, "Cheeseburger":10, "Water":4, "Coke":5} total = 0 if order in list: total = list[order] else: total = list["Coke"] sum = total + (total*0.07) print(sum)
2nd Mar 2023, 2:42 AM
Jeff McCann
+ 1
Is it "Nacho" or "Nachos"?
2nd Mar 2023, 12:22 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Technically doesnt matter, but users are certainly more likely to say “Nachos”; I must have accidentally deleted the ‘s’ and I have adjusted my previous answer to reflect “Nachos” now
2nd Mar 2023, 3:26 PM
Jeff McCann
+ 1
If you print "i" inside the for you get this N A C H O S so i guess is looping char by char and comparing every char with the array you must split the String and put the resultant 4 strings from order input in an array and then loop through the array of Strings order = input().split(" ") list = {"Nachos":6, "Pizza":6, "Cheeseburger":10, "Water":4, "Coke":5} total = 0 for i in order: if i in list: total += list[i] else: total += list ["Coke"] sum = total + ((total*7)/100) print(sum)
2nd Mar 2023, 5:01 PM
озкар п
озкар п - avatar
+ 1
Yes, a for loop on a sting iterates on each character, much like it were a list of characters. In this problem however, the list variable is technically a dictionary of key-value pairs and each value can be called by matching the name of the key
2nd Mar 2023, 6:22 PM
Jeff McCann
+ 1
# The root of your problem is that you are looping over each character of the word instead of the words. # For instance, your program is taking "Nachos Coke" as ["N", "a", "c", "h", "o", "s", " ", "C", "o", "k", "e"] instead of ["Nachos", "Coke"] # Again if, "o" is in "Nachos", it is also in "Coke", hence you are getting higher output. #to solve your problem, just use "input().split(" ") instead of "input()" def get_total(orders): #menu containing all items menu = {"Nachos" : 6.0, "Pizza" : 6.0, "Cheeseburger" : 10.0, "Water" : 4.0, "Coke" : 5.0} # using list comprehension for retrieving sum from all the items price total = sum([menu[i] if i in menu else menu["Coke"] for i in orders]) # calculates tax tax = (total / 100) * 7 # total price return total + tax # splitting the input by spaces because inputs are space separated orders = input().split(" ") print(get_total(orders)) # Please pardon my English as I am bilingual # Never discourage yourself because of small bugs.
3rd Mar 2023, 3:00 PM
Tasnim Nawar
Tasnim Nawar - avatar
0
rodwynnejones Thank you for your feedback re names. Your code doesnt pass all the tests in Code Coach. Thanks anyway!
2nd Mar 2023, 8:30 AM
Camelia
Camelia - avatar