Python Forum

Full Version: applying total in a loop to a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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: ")
(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