CSC111 randomName.py

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 16:14, 26 February 2010 (UTC)


The program below was put together (but is not finished) using the top-down approach. Students selected the problem: "Generate a list of random students with random names, and random ages, sort them up and find the oldest, and store the list in a file".

The more advanced version, created outside class with a bit more time is available here.

def getRandomStudents():
    students = [ [ "Jane", 1, 2, 1990 ],
                 [ "Joe", 2, 3, 1991 ] ]
    return students

def reorder( students ):
    newStudents = []
    for student in students:
        name, month, day, year = student
        newStudents.append( [ year, month, day, name ] )
    newStudents.sort()
    print "newS = ", newStudents
    return newStudents

def printOldest( students ):
    newS = reorder( students )
    #print students
    print "oldest = ", newS[ 0 ]

def storeToFile( students ):
    file = open( "students.txt", "w" )
    file.write( str( students ) )
    file.close()




def main():
    
    students = getRandomStudents()
    #print students

    printOldest( students )

    storeToFile( students )

main()