finding the maximum and minimum

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 3
Reputation: eplymale3043 is an unknown quantity at this point 
Solved Threads: 0
eplymale3043 eplymale3043 is offline Offline
Newbie Poster

finding the maximum and minimum

 
0
  #1
Dec 11th, 2008
Write a program that can read the students records from the file “scores.dat”. This program will find the maximum and the minimum student scores. The first line in the file is a header. Each line (starting from the second line) contains the records of one student separated by spaces. These records are: the last name, the first, name, the ID, and the score.

# ETHAN PLYMALE

def main():
infile = open(scores.dat, 'r')

# Finding the maximum


and now I do not know what to do?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: eplymale3043 is an unknown quantity at this point 
Solved Threads: 0
eplymale3043 eplymale3043 is offline Offline
Newbie Poster

Re: finding the maximum and minimum

 
0
  #2
Dec 11th, 2008
# ETHAN PLYMALE

import string

def main():
infile = open(scores.dat, 'r')
infile.readline()

# Finding the maximum
for line in infile.readlines():
line = string.split(line)
score = line[3]
print score

i tried that but its not working.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: finding the maximum and minimum

 
0
  #3
Dec 11th, 2008
Please use code tags when posting code.

Are you receiving an error message? If so, please post it.

Initialize a maximum score variable (maxscore). Loop on the file object for line in file_obj: . Strip and split each line lineList = line.strip().split() . If the value of the score is greater than maxscore, update maxscore and save the line in another variable.

An alternative would be to read all the lines, each stripped and split, into a list and sort the list.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: finding the maximum and minimum

 
0
  #4
Dec 11th, 2008
Please use code tags for your code.

You are getting close, but you need to find if the score is a new minimum or maximum each time through the loop. Something like this:
  1. infile = open("test_scores.dat", "r")
  2. mylist = infile.readlines()
  3. infile.close()
  4.  
  5. # remove the header, first item in the list
  6. del mylist[0]
  7.  
  8. max_score = 0
  9. min_score = 100
  10. for line in mylist:
  11. # split each line into a list at space/whitespace and unpack
  12. last_name, first_name, ID, score = line.split()
  13. # for testing only
  14. print last_name, first_name, ID, score
  15. # create integer scores
  16. if score.isdigit():
  17. score = int(score)
  18. # check if it is a new max
  19. if score > max_score:
  20. max_score = score
  21. # check if it is a new min
  22. elif score < min_score:
  23. min_score = score
Then just print out the max_score and min_score results.

Please study the code and the comments!
Last edited by sneekula; Dec 11th, 2008 at 1:53 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC