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
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))