| | |
finding the maximum and minimum
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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?
# ETHAN PLYMALE
def main():
infile = open(scores.dat, 'r')
# Finding the maximum
and now I do not know what to do?
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
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
An alternative would be to read all the lines, each stripped and split, into a list and sort the list.
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.
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:
Then just print out the max_score and min_score results.
Please study the code and the comments!
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)
infile = open("test_scores.dat", "r") mylist = infile.readlines() infile.close() # remove the header, first item in the list del mylist[0] max_score = 0 min_score = 100 for line in mylist: # split each line into a list at space/whitespace and unpack last_name, first_name, ID, score = line.split() # for testing only print last_name, first_name, ID, score # create integer scores if score.isdigit(): score = int(score) # check if it is a new max if score > max_score: max_score = score # check if it is a new min elif score < min_score: min_score = score
Please study the code and the comments!
Last edited by sneekula; Dec 11th, 2008 at 1:53 pm.
No one died when Clinton lied.
![]() |
Similar Threads
- Finding the lowest of five test scores (C++)
- finding extrimum of function (C++)
- LC-3 Maximum & Minimum (Assembly)
- finding the maximum and minimum (C++)
- Homework Help. (C++)
- Looking up and displaying values in linked lists (C++)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- beginner"problem in arrays help me in codes" (C)
Other Threads in the Python Forum
- Previous Thread: Neural networking python AI
- Next Thread: Number help
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt






