473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

reading a file

1
(1) Reads from the file “hw4.txt” in the local directory the SID (which is a string) and the scores (which are integers) of each student in a class. Although the number of students and the number of scores of each student are not known in advance, you may assume that:
§ All students have the same number of scores;
§ The total number of students is less than or equal to 10;
§ The number of scores of each student is less than or equal to 5.

(2) Computes the average score (as an integer) for each student. The average of each student is computed based on (score total / number of grades). If a student has no scores, her/his average is 0.
(3) Computes the average of the class (as an integer): class average = sum of averages / number of students;
(4) Computes the grade for each student. This time however the grade is “P” (Pass) or “NP” (No Pass): If the average of a student is greater than or equal to the class average, the student’s grade is “P”; otherwise the grade is “NP”
(5) Prints the class average. Subsequently prints the SID, the average score and the grade of each student. Note the output has to be formatted so that it can be easily read, but you can determine the exact formats.

These are the directions for an assignment i have to do, but i am extremely lost..this is what i have so far and i keep getting an error for x=float(score)
Expand|Select|Wrap|Line Numbers
  1. count = 0 
  2. sum = 0 
  3. s = raw_input("Enter SID score1 score2 score3...")
  4. t= s.split()
  5. file = open("mydata.dat","r")
  6. lines = file.readlines()
  7. file.close()
  8.  
  9.  
  10.  
  11. while(s != 'q'):
  12.     count = 0 
  13.     sum = 0 
  14.     t = s.split() 
  15.     for x in t: 
  16.         if (count == 0):
  17.             SID = x 
  18.         else: 
  19.             sum = sum + int(x)
  20.         count = count + 1
  21.     no_scores = count - 1
  22.      for line in lines:
  23.         scores = line.split()
  24.         count = 0
  25.         sum = 0.0
  26.         for score in scores:
  27.             if count != 0:
  28.                 x = float(scores)
  29.                 sum = sum + x
  30.                 count = count + 1
  31.             else:
  32.                 count = count + 1
  33.         count = count - 1
  34.     if (no_scores != 0):
  35.         average = sum/no_scores
  36.     else:
  37.         average = 0.0 
  38.         grade = "NP"
  39.     if (average >= 70):
  40.         grade = "P"
  41.     elif (average < 60):
  42.         grade = "NP"
  43.     print "Class Average:", average
  44.     print"SID: %1s" %(SID),"Grade:", grade
  45.     s = raw_input("Enter SID score1 score2 score3...")
Oct 29 '08 #1
1 1908
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Oct 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.