Click here to Skip to main content
14,836,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I output the total, it gives me numbers in every new column, but I need it so that it outputs only the last one. For example, this program would output:
2
5
8
but I need it to only print 8, how do I do that?

 = [int(i) for i in input().split()]
total = a[0]
for i in range(1, len(a)):
    if a[i] > a[i-1]:
        b = a[i] - a[i-1]
        total += b
        print(total)


What I have tried:

I have tried doing the total[-1] or just the sum(b), I think that the problem might be with the int at the beginning but honestly, at this point, I'm out of ideas.
Posted
Updated 19-Feb-21 11:35am

Move the print line outside the loop: remove the indentation.
   
Quote:
How do I make it so that the program outputs only the last number?

Try
Python
for i in range(1, len(a)):
    if a[i] > a[i-1]:
        b = a[i] - a[i-1]
        total += b
print(total)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900