how can i sum the integers in a list like this.
studlist = ['mario;90;80', 'denis;80;70', 'fabio;60;70']
I have tried this way but the professor is asking to do it specifically like the list above.
score= [90, 80, 80, 70 , 60, 70]
name=['mario', 'denis', 'fabio']
print('the total score of',name[0], 'is' , score[0] score[1])
print('the total score of',name[1], 'is' , score[2] score[3])
print('the total score of',name[2], 'is' , score[4] score[5])
CodePudding user response:
You can use split
to handle the strings:
studlist = ['mario;90;80', 'denis;80;70', 'fabio;60;70']
for student in studlist:
name, *scores = student.split(';')
print(f"The total score of {name} is {sum(int(s) for s in scores)}")
# The total score of mario is 170
# The total score of denis is 150
# The total score of fabio is 130
CodePudding user response:
Here is how you can do it. If I properly understood your question.
studlist = ['mario;90;80', 'denis;80;70', 'fabio;60;70']
for name_score in studlist:
name_score_list = name_score.split(";")
print('the total score of',name_score_list[0], 'is' , int(name_score_list[1]) int(name_score_list[2]))