Python Forum
Help with argument basics
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with argument basics
#1
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.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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)]:
Reply
#6
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020