EOF while parsing

2024/10/5 15:44:28
def main():NUMBER_OF_DAYS = 10NUMBER_OF_HOURS = 24data = []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] = temperaturedata[day - 1][hour - 1][1] = humidityfor i in range(NUMBER_OF_DAYS):dailyTemperatureTotal = 0dailyHumidityTotal = 0for 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

Answer

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 sysfor line in islice(sys.stdin, NUMBER_OF_DAYS * NUMBER_OF_HOURS):day, hour, temperature, humidity = map(int, line.split())data[day - 1][0] += temperaturedata[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))
https://en.xdnf.cn/q/119589.html

Related Q&A

Why is bool(x) where x is any integer equal to True

I expected bool(1) to equate to True using Python - it does - then I expected other integers to error when converted to bool but that doesnt seem to be the case:>>> x=23 #<-- replace with a…

Getting TypeError while fetching value from table using Python and Django

I am getting error while fetching value from table using Python and Django. The error is below:Exception Type: TypeError Exception Value: not all arguments converted during string formattingMy code…

ValueError: The view **** didnt return an HttpResponse object. It returned None instead

Im using Django forms to handle user input for some point on my Django app. but it keeps showing this error whenever the user tries to submit the form. ValueError: The view *my view name goes here* di…

Game Development in Python, ruby or LUA? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Problem with this error: (-215:Assertion failed) !ssize.empty() in function cv::resize OpenCV

I got stuck with this error after running resize function line: import cv2 import numpy as np import matplotlib.pyplot as pltnet = cv2.dnn.readNetFromDarknet(yolov3_custom.cfg, yolov3_custom_last.weigh…

When I run it tells me this : NameError: name lock is not defined?

• Assume that you have an array (data=[]) containing 500,000 elements and that each element has been assigned a random value between 1 and 10 (random.randint(1,10)) .for i in range (500000):data[i]…

Unable to find null bytes in Python code in Pycharm?

During copy/pasting code I often get null bytes in Python code. Python itself reports general error against module and doesnt specify location of null byte. IDE of my choice like PyCharm, doesnt have c…

remove single quotes in list, split string avoiding the quotes

Is it possible to split a string and to avoid the quotes(single)? I would like to remove the single quotes from a list(keep the list, strings and floats inside:l=[1,2,3,4.5]desired output:l=[1, 2, 3, …

Image Segmentation to Map Cracks

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my b…

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…