Python Forum
What's the logic of this question?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the logic of this question?
#1
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?
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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())
Output:
3
Reply
#4
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.
Reply
#5
(Jun-17-2020, 11:15 AM)jefsummers Wrote: pop() actually pops the last item in the list, not the first.
Yes, absolutely my mistake
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Jun-17-2020, 08:54 AM)buran Wrote: 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)

Thanks think I got it
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020