Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    6
    ReputationRep:
    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")
    exit

    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")
    exit
    allScores={} #dictionaries store things indexed by key they are hashtables
    maxScores={}
    avgScores={}
    rawScores={}
    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
    else:
    maxScores[m] = [name]
    if a in avgScores:
    avgScores[a].append(name)
    else:
    avgScores[a] = [name]

    file.close()

    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])
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Has a teacher ever helped you cheat?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • 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...
    Reply
    Hide
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.