[Tutor] (no subject)

Dave Angel davea at davea.name
Thu May 15 03:51:09 CEST 2014


On 05/14/2014 04:48 PM, JEAN MICHEL wrote:
> I'm a Python beginner

Welcome to python-tutor

> trying write a program that reads outside txt files,

What's an "outside txt file"?  And since you only one file, perhaps you 
left out the othertwo?


> 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

I'm quite surprised at that, since you're calling range with a parameter 
of line, and line is some text string, not an int.

> here is the program
>
> def calcaverage(test1,test2,test3):
>      for count in range(line):
>          curraverage=0
>          curraverage=((test1[count]+ test2[count]+ test3[count])/3)
>          currentaverage.append(curraverage)
>          if curraverage>= 90:
>              grade= "A"
>              lettergrades.append(grade)
>          else:
>              if curraverage >= 80 and curraverage < 90:
>                  grade= "B"
>                  lettergrades.append(grade)
>              else:
>                  if curraverage >= 70 and curraverage < 80:
>                      grade= "C"
>                      lettergrades.append(grade)
>                  else:
>                      if curraverage < 70:
>                          grade= "F"
>                          lettergrades.append(grade)

You forgot to start a function here.  Thus all the following code is at 
top=level, a poor practice.

> name=[]
> test1=[]
> test2=[]
> test3=[]
> averagescore=[]
> lettergrades=[]
> with open ('/period1.txt', 'r') as infile:
>      line = infile.readline()

If you're using this statement to skip the first line of the file, you 
should not bother to assign the result anywhere.  Or assign it to a 
meaningful variable like  "ignore"

      ignore = infile.readline()

>      while line in infile:
>          values = line.split()
>          name.append(values[0] + ','+ values[1])
>          while line in infile:

Whoops.  By the time this loop finishes, the outer one will find there 
are no more lines left in the file.

>              values = line.split()
>   score1=float(value[2])
>              test1.append(score1)
>              while line in infile:
>                  values = line.split()
>    score2=float(value[3])
>                  test2.append(score2)
>                  while line in infile:
>                      values = line.split()
>               score3=float(value[4])
>                      test3.append(score3)

This is the place where you should call the above data-gathering 
function (the one you didn't make a function)

Then once you've gathered the data, you should call calcaverage() with 
test1, test2,test3, and at least one more argument.

> averagescore=calcaverage(test1,test2,test3)
> infile.close()

Not needed.  infile is closed once the with clause completes.


> print(line)

Which of the many meanings of line do you mean to print out??

Those triply nested reads of the same file are doomed to failure.  Once 
the innermost one of them reaches the end of file, the others will see 
nothing more.

We can't debug it much further without knowing much more about what the 
data file (files ???) looks like.  For example, perhaps there are 
supposed to be three files, corresponding to the three lists: test1, 
test2, and test3.

-- 
DaveA


More information about the Tutor mailing list