Reputation: 6735
I have been trying to compute the total sum of all these ord
s so I have decided to use the sum()
built in function.
What happens though it outputs the sum total x4 when I run the script. Anyone know why?
T = ord('a'), ord('b'), ord('c'), ord('d')
for c in T:
c = sum(T)
print(c)
edit:
T = "hi chris"
total = 0
for c in T:
total += ord(c)
print(total)
This seems to be working but its calculation each one individually, i want 1 sum and the total, not each characters total individually...
Upvotes: 0
Views: 150
Reputation: 137290
Your for
loop is incorrect. You are re-calculating the sum of T
with each loop. Instead of doing this:
for c in T:
c = sum(T)
print(c)
do this:
print(sum(T))
or this:
s = 0
for c in T:
s += c
print(s)
Upvotes: 2
Reputation: 176730
You don't need to put the sum
in a loop. It automatically works on the whole list.
total = sum(T)
Alternatively, don't use sum
:
total = 0
for c in T:
total += c
in which case you can use a loop.
For a bit more info, see the built-in functions docs.
Upvotes: 6