For loop to print old value and sum of old value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












I am using for loop in python to display old value and sum of new value. Following is my code.



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:

sum = sum+val
print(sum)


and the output of this loop is showing 48



But i want to show output like



6
6+5 = 11
11+3 = 14
14+8 = 22
22+4 = 26
26+2 = 28
28+5 = 33
33+4 = 37
37+11 = 48


Please let me know what i need to change in my code to display output like this.










share|improve this question







New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    First thing first: Your print statement is outside the loop. This means it will only execute once.
    – Anton vBR
    Nov 18 at 8:27










  • @Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
    – Ramona
    Nov 18 at 8:30







  • 2




    Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
    – Anton vBR
    Nov 18 at 8:31










  • and dont mix spaces and tabs in python3.x
    – Patrick Artner
    Nov 18 at 8:57














up vote
6
down vote

favorite
1












I am using for loop in python to display old value and sum of new value. Following is my code.



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:

sum = sum+val
print(sum)


and the output of this loop is showing 48



But i want to show output like



6
6+5 = 11
11+3 = 14
14+8 = 22
22+4 = 26
26+2 = 28
28+5 = 33
33+4 = 37
37+11 = 48


Please let me know what i need to change in my code to display output like this.










share|improve this question







New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    First thing first: Your print statement is outside the loop. This means it will only execute once.
    – Anton vBR
    Nov 18 at 8:27










  • @Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
    – Ramona
    Nov 18 at 8:30







  • 2




    Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
    – Anton vBR
    Nov 18 at 8:31










  • and dont mix spaces and tabs in python3.x
    – Patrick Artner
    Nov 18 at 8:57












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





I am using for loop in python to display old value and sum of new value. Following is my code.



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:

sum = sum+val
print(sum)


and the output of this loop is showing 48



But i want to show output like



6
6+5 = 11
11+3 = 14
14+8 = 22
22+4 = 26
26+2 = 28
28+5 = 33
33+4 = 37
37+11 = 48


Please let me know what i need to change in my code to display output like this.










share|improve this question







New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am using for loop in python to display old value and sum of new value. Following is my code.



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:

sum = sum+val
print(sum)


and the output of this loop is showing 48



But i want to show output like



6
6+5 = 11
11+3 = 14
14+8 = 22
22+4 = 26
26+2 = 28
28+5 = 33
33+4 = 37
37+11 = 48


Please let me know what i need to change in my code to display output like this.







python python-3.x






share|improve this question







New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 18 at 8:25









Ramona

414




414




New contributor




Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ramona is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    First thing first: Your print statement is outside the loop. This means it will only execute once.
    – Anton vBR
    Nov 18 at 8:27










  • @Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
    – Ramona
    Nov 18 at 8:30







  • 2




    Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
    – Anton vBR
    Nov 18 at 8:31










  • and dont mix spaces and tabs in python3.x
    – Patrick Artner
    Nov 18 at 8:57












  • 2




    First thing first: Your print statement is outside the loop. This means it will only execute once.
    – Anton vBR
    Nov 18 at 8:27










  • @Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
    – Ramona
    Nov 18 at 8:30







  • 2




    Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
    – Anton vBR
    Nov 18 at 8:31










  • and dont mix spaces and tabs in python3.x
    – Patrick Artner
    Nov 18 at 8:57







2




2




First thing first: Your print statement is outside the loop. This means it will only execute once.
– Anton vBR
Nov 18 at 8:27




First thing first: Your print statement is outside the loop. This means it will only execute once.
– Anton vBR
Nov 18 at 8:27












@Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
– Ramona
Nov 18 at 8:30





@Anton vBR But when i print statement inside the loop then following error is showing :- File "<module1>", line 5 print(sum) ^ IndentationError: unindent does not match any outer indentation level
– Ramona
Nov 18 at 8:30





2




2




Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
– Anton vBR
Nov 18 at 8:31




Python forces you to use the correct indentation. Be careful that you have the right amount of spaces/tabs.
– Anton vBR
Nov 18 at 8:31












and dont mix spaces and tabs in python3.x
– Patrick Artner
Nov 18 at 8:57




and dont mix spaces and tabs in python3.x
– Patrick Artner
Nov 18 at 8:57












1 Answer
1






active

oldest

votes

















up vote
9
down vote



accepted










You could just iterate through elements in list, printing the required in the loop and updating the total:



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

total = numbers[0]
print(f'total')
for val in numbers[1:]:
print(f'total + val = total + val')
total += val

# 6
# 6 + 5 = 11
# 11 + 3 = 14
# 14 + 8 = 22
# 22 + 4 = 26
# 26 + 2 = 28
# 28 + 5 = 33
# 33 + 4 = 37
# 37 + 11 = 48





share|improve this answer


















  • 1




    @AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
    – Vishnudev
    Nov 18 at 8:32











  • There should be a printout before the loop
    – Mad Physicist
    Nov 18 at 8:33










  • @Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
    – Ramona
    Nov 18 at 8:35






  • 1




    @Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
    – Austin
    Nov 18 at 8:36










  • @Austin. Yes, OP's output shows 6 first
    – Mad Physicist
    Nov 18 at 8:39










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Ramona is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359072%2ffor-loop-to-print-old-value-and-sum-of-old-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote



accepted










You could just iterate through elements in list, printing the required in the loop and updating the total:



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

total = numbers[0]
print(f'total')
for val in numbers[1:]:
print(f'total + val = total + val')
total += val

# 6
# 6 + 5 = 11
# 11 + 3 = 14
# 14 + 8 = 22
# 22 + 4 = 26
# 26 + 2 = 28
# 28 + 5 = 33
# 33 + 4 = 37
# 37 + 11 = 48





share|improve this answer


















  • 1




    @AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
    – Vishnudev
    Nov 18 at 8:32











  • There should be a printout before the loop
    – Mad Physicist
    Nov 18 at 8:33










  • @Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
    – Ramona
    Nov 18 at 8:35






  • 1




    @Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
    – Austin
    Nov 18 at 8:36










  • @Austin. Yes, OP's output shows 6 first
    – Mad Physicist
    Nov 18 at 8:39














up vote
9
down vote



accepted










You could just iterate through elements in list, printing the required in the loop and updating the total:



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

total = numbers[0]
print(f'total')
for val in numbers[1:]:
print(f'total + val = total + val')
total += val

# 6
# 6 + 5 = 11
# 11 + 3 = 14
# 14 + 8 = 22
# 22 + 4 = 26
# 26 + 2 = 28
# 28 + 5 = 33
# 33 + 4 = 37
# 37 + 11 = 48





share|improve this answer


















  • 1




    @AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
    – Vishnudev
    Nov 18 at 8:32











  • There should be a printout before the loop
    – Mad Physicist
    Nov 18 at 8:33










  • @Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
    – Ramona
    Nov 18 at 8:35






  • 1




    @Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
    – Austin
    Nov 18 at 8:36










  • @Austin. Yes, OP's output shows 6 first
    – Mad Physicist
    Nov 18 at 8:39












up vote
9
down vote



accepted







up vote
9
down vote



accepted






You could just iterate through elements in list, printing the required in the loop and updating the total:



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

total = numbers[0]
print(f'total')
for val in numbers[1:]:
print(f'total + val = total + val')
total += val

# 6
# 6 + 5 = 11
# 11 + 3 = 14
# 14 + 8 = 22
# 22 + 4 = 26
# 26 + 2 = 28
# 28 + 5 = 33
# 33 + 4 = 37
# 37 + 11 = 48





share|improve this answer














You could just iterate through elements in list, printing the required in the loop and updating the total:



numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

total = numbers[0]
print(f'total')
for val in numbers[1:]:
print(f'total + val = total + val')
total += val

# 6
# 6 + 5 = 11
# 11 + 3 = 14
# 14 + 8 = 22
# 22 + 4 = 26
# 26 + 2 = 28
# 28 + 5 = 33
# 33 + 4 = 37
# 37 + 11 = 48






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 18 at 8:41

























answered Nov 18 at 8:29









Austin

8,3663828




8,3663828







  • 1




    @AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
    – Vishnudev
    Nov 18 at 8:32











  • There should be a printout before the loop
    – Mad Physicist
    Nov 18 at 8:33










  • @Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
    – Ramona
    Nov 18 at 8:35






  • 1




    @Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
    – Austin
    Nov 18 at 8:36










  • @Austin. Yes, OP's output shows 6 first
    – Mad Physicist
    Nov 18 at 8:39












  • 1




    @AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
    – Vishnudev
    Nov 18 at 8:32











  • There should be a printout before the loop
    – Mad Physicist
    Nov 18 at 8:33










  • @Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
    – Ramona
    Nov 18 at 8:35






  • 1




    @Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
    – Austin
    Nov 18 at 8:36










  • @Austin. Yes, OP's output shows 6 first
    – Mad Physicist
    Nov 18 at 8:39







1




1




@AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
– Vishnudev
Nov 18 at 8:32





@AntonvBR You're right sum is an in-built function and shouldn't be used as variable name
– Vishnudev
Nov 18 at 8:32













There should be a printout before the loop
– Mad Physicist
Nov 18 at 8:33




There should be a printout before the loop
– Mad Physicist
Nov 18 at 8:33












@Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
– Ramona
Nov 18 at 8:35




@Austin thanks its working. But one question in my mind. Please let me know what is use of f in print(f'sum + val = sum + val')?
– Ramona
Nov 18 at 8:35




1




1




@Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
– Austin
Nov 18 at 8:36




@Ramona, They are f-strings in Python, just another way of formatting strings. You can specify variable in and Python will display it's value for you.
– Austin
Nov 18 at 8:36












@Austin. Yes, OP's output shows 6 first
– Mad Physicist
Nov 18 at 8:39




@Austin. Yes, OP's output shows 6 first
– Mad Physicist
Nov 18 at 8:39










Ramona is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















Ramona is a new contributor. Be nice, and check out our Code of Conduct.












Ramona is a new contributor. Be nice, and check out our Code of Conduct.











Ramona is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359072%2ffor-loop-to-print-old-value-and-sum-of-old-value%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?