简体   繁体   中英

Why doesn't for loop run through all my readlines?

Trying to get it to run through more than 3 lines of text but the two I have now don't even work properly.

The textfile is

74,85,65,56
97,67,83,96

Here is the code I've been working on

file = open('grades.txt','r')
for x in file:
    read = file.readline()
    sep = read.split(",")
def toNumbers():
    changeNum = [eval(x) for x in sep]
    return changeNum 
def sumlist():
    total = 0
    sum(pls)
    average = sum(pls)/len(sep)
    print ("Your average is: ", average)
def main():
     sumlist()
pls = toNumbers()
main()

Output should be

Your average is: 70.0
Your average is: 85.75

What I'm getting is only this

Your average is: 85.75

When I add in a third line in my textfile I get this error

SyntaxError: unexpected EOF while parsing

The third line is also just a row of numbers in the text file and can continue from there

74,85,65,56
97,67,83,96
10,20,30,40

Output should be

Your average is: 70.0
Your average is: 85.75
Your average is: 25.0

This is simple.

for x in file:

already iterates over lines of your file, but you are ignoring the value of x which is the content of the first line. You simply grab the content of the second line with your:

 read = file.readline()

Adding another line causes error, because what you told Python to do is for each line in the file, read next line , so for loop takes every odd line into x and then every even line is stored in read . Therefore only files with even number of lines are processed without error.
You want to have this:

for read in file:
    sep = read.split(",")

EDIT: Complete example:

def toNumbers(sep):
    changeNum = [float(x) for x in sep]
    return changeNum

def sumlist(sep):
    total = 0
    sum(pls)
    average = sum(pls)/len(sep)
    print ("Your average is: ", average)

def main():
    file = open('grades.txt','r')
    for read in file:
        sep = read.split(",")
        sep = toNumbers(sep)
        sumlist(sep)

main()

I would remove your main function as it isn't really needed in your example and call toNumbers and sumlist in the for loop itself. Note that you will need to pass the variables as arguments for the function:

def toNumbers(sep):
    changeNum = [int(x) for x in sep]
    return changeNum

def sumlist(pls,sep):
    average = sum(pls)/len(sep)
    print ("Your average is: ", average)


file = open('grades.txt','r')

lines = file.readlines()
for line in lines:
    sep = line.split(",")

    pls = toNumbers(sep)
    sumlist(pls,sep)

This gives:

Your average is:  70.0
Your average is:  85.75

The code you have above doesn't even run for me in python 3. However changing the readline to readlines as below did work perfectly.

lines = file.readlines()
for line in lines:
    sep = line.split(",")

The problem is because you use file on for loop and you use file.readline() on each step. That's why when you have 3 lines it shows error. Try add another line, 4 lines, and it will show the last line.

Printing your read and sep would help you. You'll notice using your code, you'll only get the latest line because of sep assignment.

This code below would achieve what you want:

file = open('sample.txt','r')

raw_lines = file.readlines()
lines = [raw_line.strip() for raw_line in raw_lines]


def show_avg_per_line(line):
    line_list = list(map(int, line.split(",")))
    average = sum(line_list) / len(line_list)
    print("Your average is: ", average)

for line in lines:
    show_avg_per_line(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM