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)
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
+ 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.
+ 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 :)