Python Forum
applying total in a loop to a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
applying total in a loop to a list
#1
I am having trouble figuring out why my totalList is only printing the last total that is appended to the list rather than everyone that is apart of the loop?

value = int(input("Enter a numeric value: "))
if value % 2 == 0:
    even += 1
elif value % 2 == 1:
    odd += 1
cumTotal += value
print("Your current total is: %d" % cumTotal)
# Here is where the smallest and largest values start as
# the initial value that is entered
smallest = value
largest = value
# Using user input allows no mix ups with other values interfering
# with the previous variable
userInput = input("Enter a value or 'Q' to quit: ")
previous = userInput
# Here is where the while loop enters
while userInput != "Q":
    totalList = []
    if previous == value:
        duplicates = str(previous) + " " + duplicates
    previous = value
    value = int(userInput)
    cumTotal += value
    print("Your current total is: %d" % cumTotal)
    if value > largest:
        largest = value        
# This elif statement allows the possibility of the value
# to actually now be the smallest number entered
    elif value < smallest:
        smallest = value
    if value % 2 == 0:
        even += 1
    elif value % 2 == 1:
        odd += 1
    totalList.append(cumTotal)
    userInput = input("Enter a value or Q to quit: ")
Reply
#2
(Feb-11-2018, 02:59 AM)JustinxFFx Wrote:
while userInput != "Q":
    totalList = []
Because you create a new list every time you loop, so there's never more than one element in it.

Also, please don't edit out the contents of your post. The whole point of a forum is to help other people, in the future, with a similar issue, who stumble upon your question via google, and they can't get help if the post isn't there :p
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem applying conditions against results back from a database card51shor 31 31,008 Jun-13-2020, 05:33 PM
Last Post: card51shor
  Python List with Total and Average mcnhscc39 3 3,088 Nov-28-2018, 09:02 AM
Last Post: DeaD_EyE
  List showing running total of instances Pie_Fun 11 9,575 Jan-20-2017, 11:29 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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