I need to read in a list from a text file, put that list in a list, read in the amount of credits the students earned as an int, and than add 16 credits to the list.

Here is my code so far:

myList2 = []
myList = []
myFile = open('lab11.txt','r')
for myLine in myFile:
    myList = myLine.split()
    myList[3] = int(myList[3])
    myList2.append(myList)    
print myList2

Currently, I can print a list inside a list. I am not sure how to take a specific value from a list and convert it to an int. The text file appears as follows:

Max Medium MATH 58
Jane Johnson GEOG 78
Bill Bupkiss BIOL 29
Harold Humphries HLTH 43
Carol Cramer CIS 102
Alvin Adams ART 67
Fred Frederick FINA 81
Phillip Parker PHIL 44
Sam Spade SOC 16

It looks like you've done everything except add the 16 credits:

myList2 = []
myList = []

myFile = open('lab11.txt','r')
for myLine in myFile:
    myList = myLine.split()
    myList[3] = int(myList[3]) + 16
    myList2.append(myList)
    
print myList2

That doesn't work..returns a syntax error. That line wasn't completed as lab time ended and I needed to post quickly.

myList[3] = int(myList[3]) + 16

Edited 5 Years Ago by bigredaltoid: n/a

I removed some trailing white space after Sam Spade and it works (for me) on both Windows and Unix (ver 2.2.3).

I don't think you understand what I was asking, my apologies if I used poor wording. The code that I had prints a list within a list which is filled with values from the text file. I need to convert the number values to integers, add 16 to them, and than print another list with the changed values.

Ok I am confused. I opened up a new python window and wrote the code in from scratch and am now getting the desired result. I apologize Thines01, you were in fact correct. Thanks.

This question has already been answered. Start a new discussion instead.