Hey! Sign in to get help with your study questionsNew here? Join for free to post
 You are Here: Home

# 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
1. 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])

Write a reply…

Submit reply

## Register

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.

This forum is supported by:
Today on TSR

### Who is getting a uni offer this half term?

Find out which unis are hot off the mark here

Poll
Useful resources

## Make your revision easier

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups
Study resources

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.