+ 1

Ballpark Challenge

Couldn’t figure Why is my “not in” statement failing? Below is the code Help please https://code.sololearn.com/c79KoR62Ekif/?ref=app

1st Feb 2020, 3:18 PM
sandeep
sandeep - avatar
10 odpowiedzi
+ 4
Is there not in Syntax in Python? It should be like this:- if not a in items:
1st Feb 2020, 3:39 PM
A͢J
A͢J - avatar
+ 1
Hey sandeep can you let us know what you are trying to achieve with that first for-statement? Because as of now, your program is syntactically invalid due to that for-statement. Furthermore, the program contains redundant code, if my assumption is correct: you are trying to make a program that calculates the total amount to be paid for a specific order of items that you defined in a dictionary, which is kind of the 'menu'. And you want a "default value" for user input that isn't in the menu. If that assumption of mine is correct, then you can reduce the program to the below: https://code.sololearn.com/cYX0airL7XW5/?ref=app The above program defines a menu with the corresponding prices (the dictionary). It then calculates how much someone needs to pay based on their order. If the user ordered something that isn't in the menu, then the program will treat it as if the user ordered "Coke". Hope this helps.
1st Feb 2020, 4:09 PM
Hiro
Hiro - avatar
+ 1
Hiro You can tell where is problem but you can't solve Code Coach Problem publically because other people also watching this and they can take code to solve the problem but they will never learn coding so please a request next time don't solve anyone's problem here in Q/A. And sandeep This is for you also. I hope you understand well.
1st Feb 2020, 8:17 PM
A͢J
A͢J - avatar
+ 1
🅰🅹 - Sʀ. Sᴏғᴛᴡᴀʀᴇ Eɴɢɪɴᴇᴇʀ yes you're right. Thanks for the reminder.
2nd Feb 2020, 6:47 AM
Hiro
Hiro - avatar
+ 1
Hi Hiro. I know publishing the code is against the rules, but your method was really helpful for me to work through my own. I just have a couple questions so I can fully understand the approach you took. 1) How did you decide to set your sum variable to 0? Is there another way that same variable could have been created? 2) Why does the equation at the end work? That is how does setting res = sum + sum*tax actually add all of the menu items together once they've been entered? In my mind, I would have thought you'd need to use the append or join method at some pt. Sorry if these questions aren't making sense. I'm basically trying to visualise the process to understand how this code works, and I understand best when I get all the steps involved.
16th Feb 2020, 3:54 PM
Élysse Marcellin
Élysse Marcellin - avatar
+ 1
Hello Élysse Marcellin. I'm glad my answer was helpful. To answer your questions: 1) I needed a place to store the value of the total price without tax. That's why the sum variable was created. See it as a container that holds the value that we are calculating. And I set it equal to 0 because that is just how we initiate this variable. This 0 isn't something special. We just use it to create the sum variable. It's like telling the program: "hey, this variable sum now exists". And we use 0 instead of something like "None" because we want to do mathematical calculations later on, so we need a number to be stored in that variable. Secondly, you could have created the sum variable with the first value in the itms list. So you would do something like: ``sum = itms[0]``. However, this would change the for-loop, because now you have to start counting from the second item in that list. This makes your code less clear. So the ``sum = 0`` method is kind of like the conventional way of doing it.
17th Feb 2020, 4:42 AM
Hiro
Hiro - avatar
+ 1
Élysse Marcellin your second question: 2) The equation ``res = sum + sum*tax`` is nothing more than just adding tax to your total price. For example, let's say you go to a store and you buy a total of € 10, excluding tax. You have to pay those € 10 + whatever the tax is. Let's say the tax is 7%. So you have to pay € 10 + (7% of € 10). This becomes: € 10 + 0.07 * € 10, which becomes: € 10 + € 0.70 = € 10.70. So what you pay the cashier is € 10.70. The same mindset is used to calculate the res variable in my code: you have the total amount + the added tax. Take this calculation from before: € 10 + 0.07 * € 10 and change ``€ 10`` with ``sum`` and change ``0.07`` with ``tax``. That is how you get the equation in my code. Hope this helps.
17th Feb 2020, 4:52 AM
Hiro
Hiro - avatar
+ 1
Thank you Hiro! That explanation makes so much more sense to me now. So basically, the loop command is adding all the values from the dictionary and then those values are added up and entered into our "container". Then the equation takes the total value that we just added up in our "container" (aka grocery basket in my head) and adds tax. Did I get that right?
17th Feb 2020, 1:33 PM
Élysse Marcellin
Élysse Marcellin - avatar
+ 1
Élysse Marcellin exactly! :)
17th Feb 2020, 6:28 PM
Hiro
Hiro - avatar
0
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:30 AM
dendy-ua
dendy-ua - avatar