943,688 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3555
  • Python RSS
Dec 11th, 2008
0

finding the maximum and minimum

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eplymale3043 is offline Offline
3 posts
since Dec 2008
Dec 11th, 2008
0

Re: finding the maximum and minimum

# 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
eplymale3043 is offline Offline
3 posts
since Dec 2008
Dec 11th, 2008
0

Re: finding the maximum and minimum

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.
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 11th, 2008
0

Re: finding the maximum and minimum

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Neural networking python AI
Next Thread in Python Forum Timeline: Number help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC