0

Our class has been given a task to sum strings in a list.

Villains = ["The Joker","Magneto","Red Mist","Doc Ock"]

for counter in range(4):
    print(Villains[counter])

print("Wages")

Wages=["21","17","3","5"]

for counter in range(4):
    print(Villains[counter],":£",Wages[counter],"M")

TotalWage=0

for counter in range(4):
    TotalWage += Wages

That's the closest we've got so far...

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
aesthii
  • 27
  • 3
  • 2
    you might use the [`sum` builtin](https://docs.python.org/3/library/functions.html#sum), but you'd need to convert the strings to integers first... – kojiro Mar 12 '20 at 20:59
  • 4
    Does this answer your question? [Summing elements in a list](https://stackoverflow.com/questions/11344827/summing-elements-in-a-list). It even covers casting to `int`. – wjandrea Mar 12 '20 at 21:00
  • 1
    FWIW, you might learn quicker (and best practices) without the teacher from https://docs.python.org/3/tutorial/index.html – OneCricketeer Mar 12 '20 at 21:05

1 Answers1

2

Replace your last two lines with the below. You have two issues - first you're not iterating through anything using your counter in your loop, as Wages is a list and you need to call items from the list. Second the items in Wages are strings not integers, so you need to convert it.

for counter in range(4):
    TotalWage += int(Wages[counter])
katardin
  • 596
  • 3
  • 14
  • 4
    Note to OP: in a "real world" program you'd like to use function `sum()` as suggested in comments. However, I am pretty sure that @katardin's answer is the one expected by the teacher. – Błotosmętek Mar 12 '20 at 21:03
  • 1
    Do you really need the range? – OneCricketeer Mar 12 '20 at 21:03
  • @Błotosmętek Yeah that's what I thought but it says to use a for loop and I don't think you can use sum in a for loop – aesthii Mar 13 '20 at 07:33