![]() |
What's the logic of this question? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: What's the logic of this question? (/thread-27691.html) |
What's the logic of this question? - extricate - Jun-17-2020 Hi there, I have a question on this question: Cash Register Input create a empty list purchase_amounts populate the list with user input for the price of items continue adding to list with while until "done" is entered can use while True: with break print purchase_amounts continue to pt 2 #[ ] complete the Register Input task above Task4 pt 2 Cash Register Total create a subtotal variable = 0 create a while loop that runs while purchase_amount (is not empty) inside the loop pop() the last list value cast as a float type add the float value to a subtotal variable after exiting the loop print subtotal be sure to populate purchase_amounts by running pt 1 above # [ ] complete the Register Total task above --------------------------------------- This is what I've come up with from the steps guided by the assignment: purchase_amounts = [] while True: price = input("Enter price: ") if price == "done": break elif price != "done": purchase_amounts.append(price) print(purchase_amounts) subtotal = 0 while purchase_amounts != []: add3 = float(purchase_amounts.pop()) subtotal = subtotal + add3 print(subtotal)I don't understand the logic of the second part of the code. The aim is to sum of all the numbers but why do we have to do what we do? I tried to logic it out and by taking out the .pop(), it makes a difference to the answer. This lesson was on .pop() and my understanding of it is to take out an argument from a list, to remove it, but why does the .pop() function play such a significant in part 2 of the code above? RE: What's the logic of this question? - buran - Jun-17-2020 you are completely right that you can achieve the same without using pop() but as it's a lesson on using pop() - well, they use it....pop() not only removes the value at given index (default is 0), but it also returns that value, i.e. it pass the value, returned by pop() to float() and then assign that value to name add . The use of pop() is important because the loop will be executed (i.e. it will keep pop values from the list) until list is empty. If the values are not removed from the list, it will be infinite loop.Note that your assignment ask you tot pop() the last list value and you pop the first. Also empty list is evaluated as False, so it's better to do just while purchase_amounts: (if you have learned that)
RE: What's the logic of this question? - jefsummers - Jun-17-2020 pop() actually pops the last item in the list, not the first. If you call with an argument it will pop that item, but called without an argument pops the -1 argument. foo = [1,2,3] print(foo.pop())
RE: What's the logic of this question? - Godserena - Jun-17-2020 pop function is used to remove an element from the list. When an argument is given it removes the element of that index, a good use of pop can be when using stack i.e. you remove the last input element. Pop here plays a significant role as we do not have a count of items being input (as it is completely dependant on user), but you have to remove the last element. Since pop doesn't require an argument it will work for a list having n number of elements. RE: What's the logic of this question? - buran - Jun-17-2020 (Jun-17-2020, 11:15 AM)jefsummers Wrote: pop() actually pops the last item in the list, not the first.Yes, absolutely my mistake RE: What's the logic of this question? - DeaD_EyE - Jun-17-2020 pop(0) return the first element.If you use pop(0) , it's an indicator, that you want to use collections.deque .Quote:A list-like sequence optimized for data accesses near its endpoints. RE: What's the logic of this question? - extricate - Jun-18-2020 (Jun-17-2020, 08:54 AM)buran Wrote: you are completely right that you can achieve the same without using Thanks think I got it |