+ 2
Ballparks challenge
https://www.sololearn.com/coach/15?ref=app Any one who was Succeed in this challenge?
14 Answers
+ 6
I succeeded.
If you would like help you should post your code attempt :)
+ 4
Try this:
Change 'order=input()' to 'order=input().split()'. This will allow multi line input and split the input into a list
Then iterate through the list and check for each item in the dictionary. I used a 'for loop' to do this.
+ 2
Keith
This is my attempt
menu={"Pizza": 6 , "Nachos": 6 ,"Cheeseburger": 10,"Water": 4,"Coke": 5}
order=input()
if order in menu:
tax=menu.get(order)*0.07
cost=menu.get(order)+tax
print(cost)
The problem is my code is running with only single word input not multi input in same statment
+ 2
Hi, Mohammed Hassan !
Start by ensuring your code can handle multiple items from the input. Consider, for example, using a loop or comprehension to process each item in the input string. Then implement a default case for items not on the menu. You might find the ‘.get()’ method with a default value useful for this purpose (see below; a solution you can compare your code with):
print(round(1.07 * sum({"Nachos": 6, "Pizza": 6, "Cheeseburger": 10, "Water": 4}.get(i, 5) for i in input().split()), 2))
+ 2
I used a for loop and if/else statement:
# Iterate through order and calculate cost
for i in order:
if i in items_dict:
(do stuff)
else:
(do something else)
There are many ways to accomplish this task, including List Compression but this is probably easier to understand for beginners.
+ 2
Mohammed Hassan What does your code look like now? Post an update of your code so others can help you 👍
+ 2
Hope this helps:
https://sololearn.com/compiler-playground/ciz3OJ4PUUZO/?ref=app
+ 2
I modified your code and this should work:
menu={"Pizza": 6 , "Nachos": 6 ,"Cheeseburger": 10,"Water": 4,"Coke": 5}
cost=0.0
order=input().split()
#order=list(order_1)
for i in order:
if i not in menu:
tax=menu.get("Coke")*0.07
cost=cost+tax+menu.get("Coke")
else:
tax=menu.get(i)*0.07
cost=cost+menu.get(i)+tax
print(cost)
Changes I made:
1. You do not need 'order=list(order_1)' because the input statement already creates a list.
2. 'cost' should be initialized as a float, not an integer.
3. I changed the for statement to 'for i in order:', now 'i' will represent each item.
I posted this code earlier, that just needed to be completed:
https://www.sololearn.com/en/compiler-playground/ciz3OJ4PUUZO/?ref=app
Your on the right track and should be able to complete the challenge now!
+ 2
I use dictionary for menu, and loop calculation.
Here my code for reference :
https://sololearn.com/compiler-playground/cr4DEdbSK272/?ref=app
edit: oh sorry, i dont realize if this question for phyton, and im use java. but i think the logic and method maybe same
+ 1
I Already do this method but the code need much more optimization
0
Keith
Per Bratthammar
Ok if there is a item not found in menu in the for processing how can the code looks like this is my risk now
0
https://sololearn.com/compiler-playground/c2N383c2YH4N/?ref=app
Keith
These is small mistake i don this code in another application
0
Well itried it using C (rn) and still fail.