Posts: 14
Threads: 1
Joined: Dec 2017
Dec-17-2017, 09:41 PM
(This post was last modified: Dec-17-2017, 09:47 PM by klara.)
Thank you again! Haha only now I see everyone gets a name. Suddenly I became a programmer named Tim lol
so what I did now is:
def average(januar):
q = {}
for day, data in januar.items():
for city, pollution in data.items():
d = ['Texas', 'Moscow', 'Berlin', 'Athens']
if city not in q:
q[city]=0
if city in q:
q[city] += 1
avg[city]= sum(pollution)/float(len(pollution))
return avg I get:
Test 6: TypeError: 'int' object is not iterable
Test 7: TypeError: 'int' object is not iterable
Test 8: TypeError: 'int' object is not iterable
Test 9: TypeError: 'int' object is not iterable
Test 10: TypeError: 'int' object is not iterable
What should I do?
And, is it correct I change the previous k to q now so I don't overwrite the previous definition?
Posts: 2,953
Threads: 48
Joined: Sep 2016
Dec-17-2017, 09:46 PM
(This post was last modified: Dec-17-2017, 09:46 PM by wavic.)
Pay attention to what you understand better right now. Python has lots of powerful tools and I don't know them all.
Break the assignment into pieces.
First: what is an average. Sum the numbers then divide the result by their number.
So you need to keep the pollution values of each day. Bunch of values? A list of course.
What you need is to loop over each day, get the pollution for each city and put it somewhere.
You have to modify the previous solution a little bit. Instead of adding 1 to the value if the condition is true you have to just append that value to a list.
Again a dictionary with the city's names as keys and a list to keep the pollution values for each key. You don't even need an if statement. Just dict[city_name].append(pol_value). Same loop
Looping over the dict and calculating the average is the easiest part.
Posts: 69
Threads: 0
Joined: Dec 2017
for city, pollutin in data.items(): Spelling!
avg[city]= sum(pollution)/float(len(pollution)) how far is avg indented? and why float(len(pollution))? when k show how many time a city appears.
def number_of_polluted_days(januar):
k = {}
for day, data in januar.items():
for city, pollution in data.items():
d = ['Texas', 'Moscow', 'Berlin', 'Athens']
if city not in k:
k[city]=0
if city in k:
k[city] += 1
#a code to sum a pollution with respect to city
print(k) #Shows the number of time a city appears.
#the average should be calculated here
#avg[city]= sum(pollution)/float(len(pollution))
return k
januar = {
1: {'Texas': 126, 'Moscow': 64, 'Berlin': 57, 'Athens': 35,},
2: {'Texas': 53, 'Moscow': 56, 'Berlin': 58,},
3: {'Texas': 34, 'Moscow': 71, 'Berlin': 48, 'Athens': 55,}
}
number_of_polluted_days(januar)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 2,953
Threads: 48
Joined: Sep 2016
That 'd' list is there for nothing.
Posts: 14
Threads: 1
Joined: Dec 2017
Thank you Wavic for pointing that out. I thoughtlessly thought in the beginning I would need a list with all the cities.
Terafy, what do you mean by how far avg is intended?
So should I do it like this instead of what I wrote above:
avg[city]= sum(pollution)/k(pollution))
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 10:30 PM
(This post was last modified: Dec-17-2017, 10:30 PM by Terafy.)
whoops indent* this happens when you have 4 hours sleep
def number_of_polluted_days(januar):
k = {}
print('if you used sum(pollution) what are you really summing?')
for day, data in januar.items():
print('Day:',day)
for city, pollution in data.items():
#d = ['Texas', 'Moscow', 'Berlin', 'Athens']
if city not in k:
k[city]=0
if city in k:
k[city] += 1
print(city,pollution) #if you use sum(pollution)
#how far did you indent this avg? Depending on the indent might get looped
#avg[city]= sum(pollution)/float(len(pollution))
#avg[city]= sum(pollution)/float(len(pollution))
#avg[city]= sum(pollution)/float(len(pollution))
print(k) #Shows the number of time a city appears.
return k
januar = {
1: {'Texas': 126, 'Moscow': 64, 'Berlin': 57, 'Athens': 35,},
2: {'Texas': 53, 'Moscow': 56, 'Berlin': 58,},
3: {'Texas': 34, 'Moscow': 71, 'Berlin': 48, 'Athens': 55,}
}
number_of_polluted_days(januar)
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 14
Threads: 1
Joined: Dec 2017
Dec-17-2017, 10:31 PM
(This post was last modified: Dec-17-2017, 10:39 PM by klara.)
I am sorry Terafy, but you lost me in the last post. #confusedAF
wavic, I kind of understand what you're telling me but I have no idea now to put that in a program language in an order that would make sense.
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 10:44 PM
(This post was last modified: Dec-17-2017, 10:56 PM by Terafy.)
Ah sorry I'll be more direct.
You can't use sum(pollution). Because pollution will be just a number which is why you got:
Error: TypeError: 'int' object is not iterable.
And you forget that you input data (januar) contains the pollution for each city per day. You are not trying to find the cities combine average pollution per day. You trying to find average pollution of a city.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 2,953
Threads: 48
Joined: Sep 2016
def average(januar):
k = {}
# collecting the pollution values for each city
# the same loop as before
# I can't write it more clearly
for day, data in januar.items():
for city, pollution in data.items():
if k[city]: # if the city is already in the dictionary
k[city].append(pollution) # just append the pollution
else: # if it is not
k[city] = [] # create the key and put an empty list as a vallue. This list will holds all pollution values for this city
k[city].append(pollution) # then append the pollution The result:
Output: k = {
'Athens': [35, 55],
'Berlin': [57, 58, 48],
'Moscow': [64, 56, 71],
'Texas': [126, 53, 34]
}
Now you can loop over it and calculate the average easy
Posts: 14
Threads: 1
Joined: Dec 2017
Sorry for the delay, Monday came into play again and I had a ton of work @college.
Firstly, thank you everyone for your selfless help!
What I did now is:
def povprecje(januar):
k = {}
for day, data in januar.items():
for city, pollution in data.items():
if k[city]:
k[city].append(pollution)
else:
k[city]=[]
k[city].append(pollution)
for pollution in k:
k[pollution] = sum(k[pollution])/len(k[pollution])
return k Somehow this kills the whole first definition since 0 tests pass when I run them! What the...??
What are your thoughts on this? Thank you in advance!
|