[SOLVED] Function - calculate the total mark in list from user input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[SOLVED] Function - calculate the total mark in list from user input

Im trying to calculate the total of student marks from an input but it told me : "total +=x TypeError: unsupported operand type(s) for +=: 'int' and 'str'". stud_mark = input("Enter marks *separates by commas*:") def cal_total(stud_mark): total = 0 for x in stud_mark: total +=x return total cal_total(stud_mark)

15th Jun 2018, 2:49 AM
Samantha philip
Samantha philip - avatar
3 Answers
+ 1
stud_mark is a string. You should convert it to an array by doing stud_mark = input('Enter marks by comma:').split(',') now you can iterate on it
15th Jun 2018, 3:02 AM
Bebida Roja
Bebida Roja - avatar
+ 1
replace the first line by stud_mark=map(int,input('Enter marks by comma').split(',')) this is to be done because we have to convert all the number to integer as in that case they are strings.
15th Jun 2018, 3:39 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
Oh, i got it now. I tried int(input("etc")) but it gave me error. Guess i had to put the map and the split .. now i understand. Thank you :)
15th Jun 2018, 6:01 AM
Samantha philip
Samantha philip - avatar