Jun-17-2020, 08:41 AM
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:
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?