you only prompt for input once so it exits after one input.. eventually you have a loop problem. what you are doing isnt making sense. why are you adding x to the list and sorting it whether x is in the list or not? the only difference is in one case you double y
and the other you reset it to 1.
your approach is entirely unsuitable for this problem and will never yield a working solution. You are using the wrong data structure to try to work on this.
again you need to go backstart with an outline and then implment. You have a pattern established of coding blindly just trying to see where it takes you. and if you have noticed it is not taking you anywhere.
- You need an outer loop to get input from user
- You need a function that given an item from the user updates a 'grocery list' appropriately
- You need to iterate over the list to print out the results when they are done with input
Do this in phases.
First pass see if you can just build a list of the lessoninputs. don't worry about counting, dont worry about duplicates, just get your input loop working so that when they hit ctrl-d it ends and figure outprints everything in the list sorted. The way you should 'build' the list is by making a function you call for example updateGroceries(cart, item)
that will put item
into the cart
. Up to you for now what type cart is. the important part is you are trying tododo that all in a function. make an outline
Second pass: now we will work on your function that builds and then gradually implement partsmaintains the 'grocery list' previously you could have used a list or a set... we're going to use a dictionary because that is an intelligent solution to your problem. The key will be the name of the item, the value will be the count. This is pretty easy. if the item is in the cart, update the value of that item by 1, (do not add it to itself, add 1) if it not in the cart, put it in the cart with a value of 1.
Third pass: this time tidy up, capitalize the input string after you receive it and have validated that it isnt empty. Figure out how to print in order properly.
Done, pat yourself on the back.