+ 2

Ballparks challenge

https://www.sololearn.com/coach/15?ref=app Any one who was Succeed in this challenge?

18th Dec 2023, 3:17 PM
Mohammed Hassan
Mohammed Hassan - avatar
14 Answers
+ 6
I succeeded. If you would like help you should post your code attempt :)
18th Dec 2023, 3:35 PM
Keith
Keith - avatar
+ 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.
18th Dec 2023, 4:03 PM
Keith
Keith - avatar
+ 3
Keith The code running by seccssfully Percentage 98% (the my code process). I think i must move forward to complete all courses about python to gain more skills and find Another method to write it
20th Dec 2023, 7:33 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 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
18th Dec 2023, 3:57 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 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))
18th Dec 2023, 8:46 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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.
19th Dec 2023, 5:06 PM
Keith
Keith - avatar
+ 2
Mohammed Hassan What does your code look like now? Post an update of your code so others can help you 👍
19th Dec 2023, 6:28 PM
Keith
Keith - avatar
19th Dec 2023, 6:45 PM
Keith
Keith - avatar
+ 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!
19th Dec 2023, 7:24 PM
Keith
Keith - avatar
+ 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
20th Dec 2023, 7:05 AM
Jaelani Kusuma
Jaelani Kusuma - avatar
+ 1
I Already do this method but the code need much more optimization
19th Dec 2023, 5:23 PM
Mohammed Hassan
Mohammed Hassan - avatar
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
19th Dec 2023, 3:20 PM
Mohammed Hassan
Mohammed Hassan - avatar
0
https://sololearn.com/compiler-playground/c2N383c2YH4N/?ref=app Keith These is small mistake i don this code in another application
19th Dec 2023, 7:04 PM
Mohammed Hassan
Mohammed Hassan - avatar
0
Well itried it using C (rn) and still fail.
20th Dec 2023, 8:17 AM
Celvien Kurniawan
Celvien Kurniawan - avatar