Hey! Sign in to get help with your study questionsNew here? Join for free to post

Can someone explain this in detail

Announcements Posted on
Four hours left to win £100 of Amazon vouchers!! Don't miss out! Take our short survey to enter 24-10-2016
    • 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])
Write a reply…


Submit reply


Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: April 29, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

What do you wear to bed?

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

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