Reputation: 225
When i want to make a new list out of carefully selected integer variables, and ask for the sum of the new list, the answer i get is always 0.
else:
if "--" in epart:
epart = epart[2:]
esplit = [int(epart) for epart in esplit]
total.append(epart)
else:
epart = [int(epart) for epart in esplit]
total.append(epart)
print sum(total)
At the top of the code is:
total = []
How do I get the code to print the sum of the totals, if:
esplit = ["34", "-3", "5", "--4"]
for epart in esplit:
Rather then 0, the program shoud print 40
Upvotes: 0
Views: 50
Reputation: 44112
You might use "string.replace" to turn "--" into "" and then convert values into int
:
>>> esplit = ["34", "-3", "5", "--4"]
>>> sum([int(itm.replace("--", "")) for itm in esplit])
40
Upvotes: 2
Reputation: 421
esplit = ["34", "-3", "5", "--4"]
print sum([int(elem[2:]) if "--" in elem else int(elem) for elem in esplit])
Upvotes: 1