+ 4

Help with CodeCoach Challenge: BallPark Orders

Even though it works fine in the 1st case, it fails to do so in the rest. Is it possible only with Dictionary? Edit: 2nd Case -> Cheeseburger Cheeseburger Coke Water Here is the explanation of the Challange: /* You and three friends go to a baseball game and you offer to go to the concession stand for everyone. They each order one thing, and you do as well. Nachos and Pizza both cost $6.00. A Cheeseburger meal costs $10. Water is $4.00 and Coke is $5.00. Tax is 7%. Task Determine the total cost of ordering four items from the concession stand. If one of your friend’s orders something that isn't on the menu, you will order a Coke for them instead. Input Format You are given a string of the four items that you've been asked to order that are separated by spaces. Output Format You will output a number of the total cost of the food and drinks. Sample Input 'Pizza Cheeseburger Water Popcorn' Sample Output 26.75 */ https://code.sololearn.com/ckYcWuHYxU0M/?ref=app

1st Aug 2021, 2:07 AM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
15 Respuestas
+ 4
That's odd. Why are you using 'contains' tho. It might be the problem. Your loop is (string i in orders), so use it: if(i == "Nachos") try applying similar syntax to all if elements.
1st Aug 2021, 12:21 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
Try: if(i == "Nachos") { sum+=nachos; } else if(i == "Pizza ") { sum+=pizza; } else if(i == "Cheeseburger") { sum+=cheeseburger; } else if(i == "Water") { sum+=water; } else { sum+=coke; }
1st Aug 2021, 12:24 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 3
NP, I believe the problem with contains was that if you had two same items in your string, it would have only counted it once.
1st Aug 2021, 12:34 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 3
hey, I'm wayy too late to the party but nevertheless, don't you think it is better to use switch statement if you are going to do if statement with only "=="?
14th Sep 2021, 3:33 PM
Vincent Xavery Harlim
Vincent Xavery Harlim - avatar
+ 2
Oh right. Jesus! It's sometimes weird. You look for something else trying to understand what causes an error or miscalculation, and you can't see a simple mistakenly put space somewhere. 😂
1st Aug 2021, 12:49 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
+ 2
Yeah that happens a lot ☺️
1st Aug 2021, 12:51 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Yuh better. But it's not like he's gonna post the code somewhere anyways so it doesn't matter at all .....
20th Sep 2021, 7:36 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
menu = {"Pizza": 6, "Cheeseburger": 10, "Water": 4, "Coke": 5} chois = input() choise = chois.split() sum = 0 for i in choise: if i in menu: sum += menu[i] else: sum += 5 print(round((sum*1.07), 2))
6th Aug 2022, 9:28 AM
dendy-ua
dendy-ua - avatar
+ 1
It is said in the task that if there is something in the string that is not on the menu, you shall order coke. So maybe try adding else statement in the loop. Try this code: foreach(string i in orders) { if(orders.Contains("Nachos")) { sum+=nachos; } else if(orders.Contains("Pizza ")) { sum+=pizza; } else if(orders.Contains("Cheeseburger")) { sum+=cheeseburger; } else if(orders.Contains("Water")) { sum+=water; } else if(orders.Contains("Coke")) { sum+=coke; } }
1st Aug 2021, 7:43 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Aleksei Radchenkov it definitely worked. Now I'm curious why the 'contains' such a problem in calculations. Thanks a lot bro :)
1st Aug 2021, 12:32 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
+ 1
Btw remove space in "Pizza " string. (I mean the one at the end), otherwise pizza would cost 5$ 😁
1st Aug 2021, 12:43 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Why is my code working for only the first two cases arr = input().split(" ") foodstuff = { "Nachos": 6.00, "Pizza": 6.00, "Cheeseburger": 10.00, "Water": 4.00, "Coke": 5.00 } total = 0 for item in arr: if item in foodstuff: total += foodstuff[item] else: total += 5 total *= 1.07 print(total)
25th Nov 2023, 2:00 AM
Aderinola Opemipo
Aderinola Opemipo - avatar
0
I did that in my forst try but the thing is, I don't know why but, the machine calculates as if the cheeseburger equaled something higher than 10 instead of its assigned value. And i don't understand why it does that.
1st Aug 2021, 12:15 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
0
it just doesn't pass the other cases, that's all. now I'll focus on that
1st Aug 2021, 12:34 PM
Aykut AKSAKAL
Aykut AKSAKAL - avatar
0
#include <iostream> #include <string> using namespace std; int main() { string arr[]={"Nachos","Pizza","Cheeseburger","Water","Coke"}; string err[4]; int cost[]={6,6,10,4,5}; int cos[4]; string r; int b,p,u=0; double y; for(int i=0;i<4;i++){ cin>>err[i]; } for( u;u<4;u++){ for(int i=0;i<5;i++){ if(err[u]==arr[i]){ cos[u]=cost[i]; } } } for(int i=0;i<5;i++){ if(cos[i]==0){ cos[i]=cost[4]; } } for(int i=0;i<4;i++){ b=b+cos[i]; } y=b+ (((double )b*7)/100); cout<<y<<endl; };
19th Mar 2024, 7:42 PM
РАДЖАН ЖУМАНОВИЧ
РАДЖАН ЖУМАНОВИЧ - avatar