Turn on thread page Beta

Can someone explain this in detail watch

    • Thread Starter

    classname = input("Which class are you in? x, y, or z ")

    if not classname in ['x', 'y', 'z']list of classnames and boolean
    print ("That class does not exist - exiting")

    filename = classname +".txt" # This code names the text according to what the user inputs as the class

    file = open(filename , 'r') #opens the file in read mode

    sortField = input("How ould you like to sort the data - alphabetically, by highest score, or by average score? Choose A, H, or V ") #input which field to use

    if not sortField in ['A', 'H', 'V']: #boolean and list of sort fields
    print ("That sort field does not exist - exiting")
    allScores={} #dictionaries store things indexed by key they are hashtables
    for line in file: # for loop
    f = line.split() # turns line separated by whitespace into a list

    (firstName, lastName, score1, score2, score3) = [t(s) for t,s in zip((str,str,int,int,int), f)]#zip #extracting fields into variable with the correct type #tuple
    name = firstName + ' ' + lastName
    scores = [score1, score2, score3]
    rawScores[name] = scores
    m = max(scores)
    a = sum(scores) / 3.0
    allScores[name] = m
    if m in maxScores:
    maxScores[m].append(name)#add to the end #bug #names are remembered # previously overwritten
    maxScores[m] = [name]
    if a in avgScores:
    avgScores[a] = [name]


    if sortField == 'A':
    d = allScores #first dictionary for name
    r = False
    elif sortField == 'H':
    d = maxScores # second dictionary for maxScores
    r = True
    elif sortField == 'V':
    d = avgScores #Third dictionary for avgScores
    r = True

    print ("Class " + classname)
    for i in sorted(d.keys(), reverse=r)sort the dictionaries by keys
    print (i, d[i])
Submit reply
Turn on thread page Beta
Updated: April 29, 2016

University open days

  • University of East Anglia
    All Departments Open 13:00-17:00. Find out more about our diverse range of subject areas and career progression in the Arts & Humanities, Social Sciences, Medicine & Health Sciences, and the Sciences. Postgraduate
    Wed, 30 Jan '19
  • Aston University
    Postgraduate Open Day Postgraduate
    Wed, 30 Jan '19
  • Solent University
    Careers in maritime Undergraduate
    Sat, 2 Feb '19
Brexit: Given the chance now, would you vote leave or remain?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.