Hey guys, I need to print a dictionary from a text file and than print it again with the last two values updated in each row.

The text file appears as so:

12345678 Joe Dokes IT 30 95
87654321 Sally Sue ISYS 50 87
34876293 Bo Burnham MATH 60 78
98375498 Yarg Noreamac PWN 45 97

I was able to print the initial dictionary, and at least start to set up the second dictionary, but I am a little confused as to how to ask the user to update the credits (30, 50, 60, 45) and the number of quality points (95, 87, 78, 97) four times.

myDict = {}
myDict2 = {}





def main():

    myFile = open('prog3.txt','r')

    for line in myFile:
        techID, first, last, major, credit, quality = line.split()
        stuRec = [first, last, major, credit, quality]
        myDict[techID] = stuRec
    print myDict

main()






def submain():
    
    myFile = open('prog3.txt', 'r')

    
    credit2 = raw_input("Please enter the updated credits.")
    update2 = raw_input("Please enter the updated quality points.")

    for line in myFile:
        techID, first, last, major, credit, quality = line.split()
        stuRec = [first, last, major, credit, quality]
        myDict2[techID] = stuRec
    print myDict2

submain()

Here is one way do accomplish this task:

import pprint

# constants are pos in list
FIRST = 0
LAST = 1
CREDIT = 3

def update_dict(myDict):
    updatedDict = myDict.copy()
    for key in myDict:
        #print key, myDict[key], myDict[key][3]  # test
        # show full name
        print myDict[key][FIRST], myDict[key][LAST]
        credit2 = raw_input("Please enter the updated credit: ")
        updatedDict[key][CREDIT] = credit2
        # also update quality points similarly
    pprint.pprint(updatedDict)  # test
    # save the updated dictionary as a pickle object


myDict = {
'12345678': ['Joe', 'Dokes', 'IT', '30', '95'],
'34876293': ['Bo', 'Burnham', 'MATH', '60', '78'],
'87654321': ['Sally', 'Sue', 'ISYS', '50', '87'],
'98375498': ['Yarg', 'Noreamac', 'PWN', '45', '97']
}

update_dict(myDict)

""" my result -->
Joe Dokes
Please enter the updated credit: 10
Sally Sue
Please enter the updated credit: 20
Bo Burnham
Please enter the updated credit: 30
Yarg Noreamac
Please enter the updated credit: 40
{'12345678': ['Joe', 'Dokes', 'IT', '10', '95'],
 '34876293': ['Bo', 'Burnham', 'MATH', '30', '78'],
 '87654321': ['Sally', 'Sue', 'ISYS', '20', '87'],
 '98375498': ['Yarg', 'Noreamac', 'PWN', '40', '97']}
"""

I am a little confused as to your code..we haven't looked at pprint at all in class. Is that a feature on Python 2.7?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.