+ 1

Rely struggling with Ballpark Orders

Hi, I am really struggling with the ballpark orders problem. I know what I want to do and even though I've googled a lot I can't quite put the pieces together to make functional code. Is my psuedocode is: - create a menu (dictionary with key:values of items: prices) - define a check() function to check if the input contains a key item in the dictionary. If input does contain a key to then retrieve the value and return it. If it does not contain a key to replace it with the coke key or to just add 6. - take a input from the user - run check() on user input - total the values returned - add tax - print final number. So far I've created a dictionary for the menu, and no I'm stuck on my check() function where I can't work out how to check the input for 4 items. So u need to run it 4 times, do I need to specify somewhere that check() should take four arguments, should I specify that the input should be four arguments? I'm feeling a bit lost, am I on the right track?

20th Aug 2024, 2:01 PM
Half Dragoness
Half Dragoness - avatar
11 odpowiedzi
+ 3
Half Dragoness , can you link your current code attempt here? it seems better to follow the code then your description.
20th Aug 2024, 2:06 PM
Lothar
Lothar - avatar
+ 3
Looks good so far! Though I think you don't need the cap_list part. One way to approach the task is this: * Create a price variable and initialize it with price = 0 * Use a loop to iterate order_list * On each iteration, check if the order item is in the menu. If yes, add the item to the price variable; if not, get your friend something else as instructed. For this part, the dictionary method .get() comes in handy: https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_dictionary_get.asp It gets value by key but also lets you specify a value for keys that aren't found in the menu (saves you an if-else statement) * After the loop, the price variable contains the sum. Calculate and add the taxes, then round()
24th Aug 2024, 2:36 PM
Lisa
Lisa - avatar
+ 2
menu = { "cheeseburger" : 10, "coke" : 6, "nachos" : 6, "pizza" : 6, "water" : 4 } def check(x): for x in menu: return x.values() order = input() total = check(order) print (total) This is what I have so far but I know it's not right and I know I haven't tried to total anything or add tax. I'm mostly stuck at the part where I check user input against the dictionary.
20th Aug 2024, 2:50 PM
Half Dragoness
Half Dragoness - avatar
+ 2
Half Dragoness , as far as i remember, the final price has to be rounded to 2 decimal places ???
20th Aug 2024, 3:17 PM
Lothar
Lothar - avatar
+ 2
King Socrates The thread concerns Python, not C. It is not helpful to give ready-made code.
21st Aug 2024, 2:45 PM
Lisa
Lisa - avatar
+ 1
"order" is a string that contains multiple items. you can split it into a list of items, using order.split() mind the spelling of the items and orders: they are capitalized. consider the case that there is an item in the order list which is not on the menu.
20th Aug 2024, 3:01 PM
Lisa
Lisa - avatar
+ 1
Hi, Half Dragoness, Why it’s not working: • The check(x) function doesn’t actually check the input against the dictionary. It just returns values immediately without checking anything. • You’re not processing multiple items or summing totals. Hints: 1. Split input into individual items. 2. Loop through each item, check if it’s in the dictionary. 3. If not in the dictionary, add the price of “coke” or 6. 4. Sum up the total. 5. Apply tax. 6. Print the final amount.
20th Aug 2024, 3:07 PM
Per Bratthammar
Per Bratthammar - avatar
0
Here is a simple C code #include <stdio.h> #include <string.h> int main() { char order[100]; float total = 0; fgets(order, sizeof(order), stdin); char *items[4]; items[0] = strtok(order, " \n"); for (int i = 1; i < 4; i++) { items[i] = strtok(NULL, " \n"); } for (int i = 0; i < 4; i++) { if (strcmp(items[i], "Nachos") == 0 || strcmp(items[i], "Pizza") == 0) { total += 6.00; } else if (strcmp(items[i], "Cheeseburger") == 0) { total += 10.00; } else if (strcmp(items[i], "Water") == 0) { total += 4.00; } else if (strcmp(items[i], "Coke") == 0) { total += 5.00; } else { total += 5.00; // Coke (if item is not on the menu) } } total *= 1.07; // 7% tax printf("$%.2f\n", total); return 0; }
21st Aug 2024, 2:44 PM
King Socrates
King Socrates - avatar
0
It's easy... If It's correct then it’s ok.. But what the fuck happens to some other... The code is right but it doesn’t fit with the test😴 Dunno my programming is wrong or I'm wrong for programming 😴
21st Aug 2024, 8:44 PM
Bayazid Habib Siddikee
Bayazid Habib Siddikee - avatar
0
I've taken ages to respond to this because I was trying to get Python and an Ide set up on a laptop so I can test out my code there rather than swapping back and forth between the puzzle and code playground here. I find it helpful to be able to run my code and print various aspect of it so I can see if the code is doing what I expect at certain points, but you can't do that in the code playground.
22nd Aug 2024, 11:13 AM
Half Dragoness
Half Dragoness - avatar
0
Hi so I've been trying to sort out python and visual studio on a laptop, and I now have this as my code so far: menu = { "Cheeseburger" : 10, "Coke" : 6, "Nachos" : 6, "Pizza" : 6, "Water" : 4 } order = input() order_list = order.split() cap_list = word.capitalize() [for word in order_list] I know that what I need to do now is check cap_list against items in the dictionary and to then pull their values out and add them together. (then add tax) I've been googling "python how to check is user input matches dictionary items" but I'm not getting any kind of clear answer. Could someone please explain how I would check if multiple user input strings are keys in my dictionary. And once I've done that how do I pull out the correct value associated with the key?
24th Aug 2024, 11:01 AM
Half Dragoness
Half Dragoness - avatar