[Tutor] (no subject)
Cameron Simpson
cs at zip.com.au
Thu May 15 01:05:59 CEST 2014
Before we begin, two etiquette requests:
- please supply a meaningful subject line; it helps everyone keep track of the discussions; this one might have used something like "help with simple grading problem"
- please see if your mailer (GMail?) can be told to send just plain text to this list
Anyway, to the topic...
On 14May2014 13:48, JEAN MICHEL <jeanrmichel at gmail.com> wrote:
>I'm a Python beginner trying write a program that reads outside txt files,
>takes the data like the name and test grades of students then calculate the
>average and also assigns a grade and writes the data into a new txt file.
>I'm having difficulties writing the program so far I've been able to write
>up half of the program but I am noticing there might be bugs. I've tried
>running the program but every time I do, my program output is blank and
>there is no error messages
In there circumstances it can be helpful to insert "print" statements in
various places in the program to see what parts of it are actually being run,
and with what values.
>here is the program
>
>def calcaverage(test1,test2,test3):
> for count in range(line):
The first thing that catches my eye is that "line" is not a variable in this
function. You clearly mean it to have a value, but it is not supplied. Normally
it would come in as a parameter with "test1, test2, test3".
On further inspection, you don't use "line", and the function does not return
"grade".
In Python, all variables in a function are, generally, "local" variables; this
is good practice in most other languages too - it means that you can look at a
function on its own without knowing much about the rest of the program, and
also means that what happens inside the function stay inside the function and
does not accidentally affect the rest of the program.
It looks like your function here calculates values from the [count] element of
the three test lists. So I would advocate:
- get rid of the "for" loop - it belongs in your main program, not here
- pass in "count" and "lettergrades" as parameters to the function - it needs to be given them
- return "grade" from the function, by putting:
return grade
at the bottom
[...snip...]
>name=[]
>test1=[]
>test2=[]
>test3=[]
>averagescore=[]
>lettergrades=[]
>with open ('/period1.txt', 'r') as infile:
You may mean just "period1.txt" here? On a UNIX system "/period1.txt" would be
at the top of the system directory tree, not in your current directory.
> line = infile.readline()
> while line in infile:
I suspect this is where your problem with no output lies.
I would expect you want to iterate over every line in the input file.
What you are actually doing above is two things:
- reading the first line of the file, just once
- examining the rest of the file to see if the line occurs later (presumably not)
Because an open text file is iterable, your while condition:
line in infile
actually loads the rest of the file to see if "line" is "in" it. That reads
everything. And becuase (probably) the line does not occur a second time, the
while loop does not every run once, thus no output.
Replace these two lines with this:
for line in infile:
which will read each line in turn for processing. Then see what happens.
Note that you do this again lower down. I am not sure what you intend to happen
here. Put it "print" statements to see what actually happens - it should help
you figure out what you really need to do.
[...snip...]
>infile.close()
You do not need to "infile.close()". The original:
with open ('/period1.txt', 'r') as infile:
will do that for you.
>print(line)
This is at the bottom, outside all the loops. It can at best print the last
line of the file. I do not know what you actaully intend here.
See if any of these suggestions help you progress with your program, and report
back (by replying to this thread) when you have new problems or new questions.
Cheers,
Cameron Simpson <cs at zip.com.au>
More information about the Tutor
mailing list