Do not overuse if
, instead write:
with open('class{}.txt'.format(class_number), 'a') as myFile:
myFile.writelines(userscore)
myFile.close() # Not necessary as Barry noted
The class number 1
will be saved in class1.txt
, the number 2
in class2.txt
...