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
9 Réponses
+ 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.
+ 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)
+ 1
Is it "Nacho" or "Nachos"?
+ 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
+ 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)
+ 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
+ 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.
0
rodwynnejones Thank you for your feedback re names.
Your code doesnt pass all the tests in Code Coach. Thanks anyway!