+ 1

[SOLVED] Ballpark Challenge

I managed to solved the challenge but something baffled me, i know it is usually a good practice to remove spaces in your usual string inputs and I kinda did meddling with the code out of curiousity as i wonder "FOR loop iterated through words and its IF condition job to find if there is an exact matching string in the iterations an then adding accordingly to total prices, so i guess split won't be needed too much right?", where thats when my assumption went wrong, the calculations became wild as like the food stall clerk is greedy enough to sneakily mark up the prices. Now my question is why does the calculation became wrong if i did not split the input first? https://sololearn.com/compiler-playground/cxNvUUGz8QO1/?ref=app

4th May 2024, 2:48 PM
SeaWater
SeaWater - avatar
6 odpowiedzi
+ 2
In other words, by not splitting, you are going from, “I’d like a coke and fries,” to, “I’d like a C, an O, a K, an E, a space, an F, an R, an I, an E, and an S.” The solution doesn’t have menu entries for any of those, so it assumes the price of coke for each.
4th May 2024, 5:41 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 5
SeaWater , an additional remark to an issue that can happen with solution2(): the check if an input matches with a member in the `menu` dict is currently done like: if items in 'cheeseburger': this means when an input like `burger` or `cheese` is given, both will be seen as correct when using the above code. it is recommended to use the == operator like: if items == 'cheeseburger': when you run the current code with an input that contains `burger water pizza pizza`, you will get a different output result compared to the function `solution1()`
5th May 2024, 3:08 PM
Lothar
Lothar - avatar
+ 4
SeaWater , just let me give 2 remarks to your code: (1) both functions are using a parameter called `totalprice`. in both of the function calls, a value of `0` is passed as an argument to the function. this way of creating / initializing the variable is not recommended. we can use like this in the functions: totalprice = 0 (2) the `solution2()` function is using some individual `if` statements with a following `continue` to check the input data. it is recommended to use this construct instead (it does not require `continue` statements): if ...: ... elif ...: ... else: ...
5th May 2024, 1:14 PM
Lothar
Lothar - avatar
+ 2
I had to read the question over and over to figure out what you were trying to say. But, I think what you said is that you want to know, why is it that when you remove the split method from the input string, the result is so different? Well, splitting a string breaks it into a list of multiple strings, the break points being wherever there was whitespace. If you don’t split the input, the solutions are handed one long string rather than a list. When Python iterates over a string, it doesn’t iterate over words, it iterates over individual characters. So, the way the solutions are written to handle the order, they are now treating each character as an item. Since no one character is in the menu (only complete words), the items will all default to the price of Coke, but now entering “cheeseburger” will be equivalent to one coke for every letter, I.E. 12 cokes. Not to mention each space is also treated as an item.
4th May 2024, 5:37 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Wilbur Jaywright, Thanks for the detailed explanation it's just like you said, i did tried meddling with my code again and just like you said if its not being split, it showed exactly like your example when i printed the iterations from the loop.
4th May 2024, 6:08 PM
SeaWater
SeaWater - avatar
+ 1
Lothar, Thanks for the input now for the remarks: 1. Is it to avoid variable confusion? , i heard that that there are other programming language that got confused if we did this and why is that can happen? Also what are your tips for identifying whether we can simply putting the temporary variable outside the function or we must putting it inside the function. 2. I see, thank you. Creating multiple IF is a bit 'dumb dumb' huh? 😬
6th May 2024, 2:44 AM
SeaWater
SeaWater - avatar