[Tutor] (no subject)
Alan Gauld
alan.gauld at btinternet.com
Thu May 15 00:51:08 CEST 2014
On 14/05/14 21:48, JEAN MICHEL wrote:
> I'm a Python beginner
Welcome to the list.
Please always include a subject line relating to your question.
It makes searching the archives much easier!
> .... 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.
Get used to it, it's impossible to write more than a
few lines of code without introducing bugs.
The trick is to get rid of them before you give the code
to anyone else! ;-)
I've tried running the program but every time I do, my program
> output is blank and there is no error messages
Try inserting some print statements to track where you have reached.
For example one just as you enter your function could show you
how often the function is being called and with what values....
Although you only seem to call it once I notice...
Thee are a couple of things below that can drive you crazy,
please don't do them.
See comments below
> def calcaverage(test1,test2,test3):
> for count in range(line):
Where is line coming from? What is its value.
Maybe a print here would surprise you?
And what does range(line) produce?
Again maybe not what you expect?
If you want to use a value in a function pass
it in as a parameter...
> curraverage=0
This does nothing useful since you overwrite
curraverage in the very next line.
> curraverage=((test1[count]+ test2[count]+ test3[count])/3)
> currentaverage.append(curraverage)
Where is currentaverage defined? Apparently its a list
but I don't see it below...
> if curraverage>= 90:
> grade= "A"
> lettergrades.append(grade)
> else:
> if curraverage >= 80 and curraverage < 90:
You can save a little typing by saying this as:
if 80 <= curraverage < 90:
> grade= "B"
> lettergrades.append(grade)
> else:
> if curraverage >= 70 and curraverage < 80:
And you can save some more as well as some indenting by using elif:
instead of else:... if:
> name=[]
> test1=[]
> test2=[]
> test3=[]
> averagescore=[]
> lettergrades=[]
> with open ('/period1.txt', 'r') as infile:
> line = infile.readline()
> while line in infile:
The easiest way to process lines in Python is to
use a for loop:
for line in infile:
> values = line.split()
> name.append(values[0] + ','+ values[1])
> while line in infile:
This definition of line will overwrite the value you had previously.
Is that really what you want? Using the same variable to hold two
different values in a single loop is usually a bad idea.
> values = line.split()
> score1=float(value[2])
> test1.append(score1)
> while line in infile:
And doing it 3 times is just asking for trouble!
Especially since you are trying to iterate over
the same file each time!
> values = line.split()
> score2=float(value[3])
> test2.append(score2)
> while line in infile:
And now 4 times. Eeek! How do you keep track
of what line's value is?
I suspect you need to unwind all these loops and rethink
the structure here. Too complicated for me to try
and read...
> values = line.split()
> score3=float(value[4])
> test3.append(score3)
> averagescore=calcaverage(test1,test2,test3)
> infile.close()
You don't need to close a file you opened using 'with'
Part of the 'with' magic is that it closes the file for you.
> print(line)
I suspect this print is way to late to help you.
Try printing line (along with a label saying which
while loop it is) after each while statement.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list