简体   繁体   中英

Why the total I am getting is wrong?

total = 0
for e in range(1, 5):
    total += e
print(e)

Output:

4

c = range(1,5)
total = 0
for e in c:
    total = total + c
print(c)

Output:

4

a = list(range(1, 5))
total2 = 0
for e in a:
    total2 = total2 + e
print(e)

Output:

4

Based on the title, i think you want to print the sum, so you should print the variable total or total2 , not the iterator.

total = 0
for e in range(1, 5):
    total += e
print(total)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM