EOF while parsing

江枫思渺然 提交于 2021-02-08 12:06:13

问题


def main():
    NUMBER_OF_DAYS = 10
    NUMBER_OF_HOURS = 24
    data = []
    for i in range(NUMBER_OF_DAYS):
        data.append([])
        for j in range(NUMBER_OF_HOURS):
            data[i].append([])
            data[i][j].append(0)
            data[i][j].append(0)
    for k in range(NUMBER_OF_DAYS * NUMBER_OF_HOURS):
        line = input().strip().split()
        day = eval(line[0])
        hour = eval(line[1])
        temperature = eval(line[2])
        humidity = eval(line[3])
        data[day - 1][hour - 1][0] = temperature
        data[day - 1][hour - 1][1] = humidity
    for i in range(NUMBER_OF_DAYS):
        dailyTemperatureTotal = 0
        dailyHumidityTotal = 0
        for j in range(NUMBER_OF_DAYS):
            dailyTemperatureTotal += data[i][j][0]
            dailyHumidityTotal += data[i][j][1]
        print("Day " + str(i) + "'s average temperature is" + str(dailyTemperatureTotal / NUMBER_OF_HOURS))
        print("Day " + str(i) + "'s average humidity is" + str(dailyHumidityTotal / NUMBER_OF_HOURS))

main()

Ok this stressing me out. I can't seem to get this code to run because of another error I am facing. What is this EOF while parsing. It seems to highlight the day = eval (line[0]) for some reason and I have no clue why


回答1:


It means line[0] consists of an incomplete Python statement. An empty string would do that, for example:

>>> eval('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

If your inputs consist of integers, use int() instead for clearer error messages, and check for empty lines.

There are better ways to build your data structure; a list comprehension for example:

totals = [[0, 0] for _ in range(NUMBER_OF_DAYS)]

This structure is enough to hold all the totals; there is no point in keeping per-hour values when you can just sum the whole thing per day instead.

I'd read from stdin instead of using input(), summing the temperature and humidity per day directly:

from itertools import islice
import sys

for line in islice(sys.stdin, NUMBER_OF_DAYS * NUMBER_OF_HOURS):
    day, hour, temperature, humidity = map(int, line.split())
    data[day - 1][0] += temperature
    data[day - 1][1] += humidity

and calculating the averages becomes:

for i, (temp, humidity) in enumerate(data):
    print("Day {}'s average temperature is {}".format(i,  temp / NUMBER_OF_HOURS))
    print("Day {}'s average humidity is {}".format(i,  humidity / NUMBER_OF_HOURS))



回答2:


Looks like that the problem lies in this section

for k in range(NUMBER_OF_DAYS * NUMBER_OF_HOURS):
        line = input().strip().split()
        day = eval(line[0])

I would assume you want a user each time to input a value and than process the input by eval?

I am not sure what eval is doing, but according to the error message the line variable is empty, or has len(line) == 0: True properties so that line[0] doesn't work.

If you use input() in python3, in contrast to python2's input(), it always reads the user input in as a string. So it is similar to python2's raw_input() function. Maybe your eval() function is missing the conversion from str to int? But again, I am not sure what eval() is doing.

Maybe just insert a

print("\n\nline: {}\n\n".format(line))

after

for k in range(NUMBER_OF_DAYS * NUMBER_OF_HOURS):
        line = input().strip().split()

and we will know more



来源:https://stackoverflow.com/questions/17842165/eof-while-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!