Posts: 10
Threads: 3
Joined: Oct 2018
Feb-14-2021, 11:58 PM
(This post was last modified: Feb-14-2021, 11:58 PM by mickandralphscrier.)
Hi there,
so I'm helping a schoolmate with her python homework and it's a bit beyond me.
I have a list of different values (country, year, average temperature). The function arguments are a specific country, the first year of temperature measurement and the last year of temperature measurement. I need to calculate the average temperature of a given year in a given country with the following formula:
Imgur link
English is not my primary language, so if anything is unclear, please let me know. I would appreciate any help with this.
Thanks.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Feb-15-2021, 07:28 AM
(This post was last modified: Feb-15-2021, 07:28 AM by buran.)
Please, don't post images of code, data, erros, etc. Copy/paste here. Use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
We are glad to help, but we are not going to do the homework for you. Post your code (what have you tried, incl. sample input), ask specific questions.
Posts: 10
Threads: 3
Joined: Oct 2018
(Feb-15-2021, 07:28 AM)buran Wrote: Please, don't post images of code, data, erros, etc. Copy/paste here. Use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
We are glad to help, but we are not going to do the homework for you. Post your code (what have you tried, incl. sample input), ask specific questions.
Sorry about that, I will keep that in mind the next time I seek help. I have since written the following code:
def avg_temp(country, first_year, last_year):
sum = 0
for cnt, year, temp in list_pod:
if cnt == country and year in range(first_year, last_year + 1):
sum += temp - 1
return sum / (last_year - first_year + 1) I realize that it's still incomplete. A friend of mine said that I'd need to add a counter, but I'm not sure how to do that.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Feb-15-2021, 08:41 PM
(This post was last modified: Feb-15-2021, 08:41 PM by buran.)
So, first of all - show what your data look like, what kind of data structure you have them
Don't use sum as name - it's a built-in function
why substract 1 from temperature?
(last_year - first_year + 1) will work if you have data for every year, is that the case?
overall you are on right path
Posts: 6,800
Threads: 20
Joined: Feb 2020
A better way is to collect the data and then process.
import statistics
def avg_temp(country, start, end):
temps = []
for cnt, year, temp in list_pod:
if cnt == country and year in range(start, end+1):
temps.append(temp)
return statistics.mean(temps) If you don't want to use the statistics library you can replace the last line with:
return sum(temps) / len(temps) Or you could get rid of the function and use the statistics library and a list comprehension.
avg_temp = statistics.mean[t for c, y, t in list_pod \
if c == country and y in range(start, end+1)]:
Posts: 10
Threads: 3
Joined: Oct 2018
Feb-17-2021, 10:11 PM
(This post was last modified: Feb-17-2021, 10:11 PM by mickandralphscrier.)
import csv
list_pod = []
with open('temp_2005.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
i = 0
for row in csv_reader:
if i > 0:
list_pod.append({
"year": int(row[0]),
"country": row[1],
"temperature": float(row[2].split("+-")[0])
})
i += 1
print(list_pod)
def avg_temp(list_pod, country, first_year, last_year):
sum = 0
count = 0
for row in list_pod:
if row["country"] == country and row["year"] in range(first_year, last_year + 1):
sum += row["temperature"]
count += 1
return sum / count if count > 0 else 0 So after going through a couple of iterations utilizing your guys' help, I ended up with this. Going to test it soon to see if it works. Thanks for everyone chiming in.
|