For loop to print old value and sum of old value
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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
New contributor
add a comment |
up vote
6
down vote
favorite
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
New contributor
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
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
New contributor
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
python python-3.x
New contributor
New contributor
New contributor
asked Nov 18 at 8:25
Ramona
414
414
New contributor
New contributor
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
add a comment |
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
add a comment |
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
1
@AntonvBR You're rightsum
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 inand 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
|
show 1 more comment
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
1
@AntonvBR You're rightsum
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 inand 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
|
show 1 more comment
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
1
@AntonvBR You're rightsum
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 inand 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
|
show 1 more comment
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
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
edited Nov 18 at 8:41
answered Nov 18 at 8:29
Austin
8,3663828
8,3663828
1
@AntonvBR You're rightsum
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 inand 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
|
show 1 more comment
1
@AntonvBR You're rightsum
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 inand 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
|
show 1 more comment
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.
Ramona is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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