Posts: 3
Threads: 1
Joined: Mar 2020
Mar-03-2020, 12:59 AM
(This post was last modified: Mar-03-2020, 01:06 AM by snippsat.)
very new to python and it is also my first programming langue. i have two list one to take in user input for how much rainfall happened each month and the other for the months of the year. i want to output: each month with how much rainfall it had, the total rainfall for the year, the avg rainfall each month, and i want to add the max and min rainfall months. i now how to use max and min command built in and can get it to print but i don't know how to get the associated month to print with it. any help would be amazing.
for clarification: if march was the heaviest rainfall month of the year and it had 10 inches i would like the output to say "March had the highest rainfall of the year with 10 inches"
rainfall = []
months = ['January', 'February', 'March', 'April', 'May',
'June','July', 'August', 'September', 'October', 'November', 'December']
rainfall_year = 0
for x in months:
day_rainfall = float(input(f" In {x} what was the rainfall for the month? "))
rainfall.append(day_rainfall)
rainfall_year += day_rainfall
monthly_rainfall_avg = rainfall_year/12
for x in range(len(months)):
print(f'{months[x]}: {rainfall[x]}')
print(f'Your total rainfall for the year is {rainfall_year}')
print(F'The monthly average rainfall for the year is: {monthly_rainfall_avg}')
Posts: 12,137
Threads: 496
Joined: Sep 2016
Mar-03-2020, 01:39 AM
(This post was last modified: Mar-03-2020, 01:39 AM by Larz60+.)
You can zip the two lists, Example:
>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May',
... 'June','July', 'August', 'September', 'October', 'November', 'December']
>>> rpt = zip(months, rainfall)
>>> for month, rainamt in rpt:
... print(f"Month: {month}, rainfall: {rainamt}")
...
Month: January, rainfall: 1.1
Month: February, rainfall: 2.3
Month: March, rainfall: 0.5
Month: April, rainfall: 2
Month: May, rainfall: 3.1
Month: June, rainfall: 0.25
Month: July, rainfall: 1.1
Month: August, rainfall: 2.3
Month: September, rainfall: 0.5
Month: October, rainfall: 2
Month: November, rainfall: 3.1
Month: December, rainfall: 0.25
Posts: 3
Threads: 1
Joined: Mar 2020
(Mar-03-2020, 01:39 AM)Larz60+ Wrote: You can zip the two lists, Example:
>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May',
... 'June','July', 'August', 'September', 'October', 'November', 'December']
>>> rpt = zip(months, rainfall)
>>> for month, rainamt in rpt:
... print(f"Month: {month}, rainfall: {rainamt}")
...
Month: January, rainfall: 1.1
Month: February, rainfall: 2.3
Month: March, rainfall: 0.5
Month: April, rainfall: 2
Month: May, rainfall: 3.1
Month: June, rainfall: 0.25
Month: July, rainfall: 1.1
Month: August, rainfall: 2.3
Month: September, rainfall: 0.5
Month: October, rainfall: 2
Month: November, rainfall: 3.1
Month: December, rainfall: 0.25
i am fine with printing the month and amount for each month. i am having issues trying to print the max rainfall with its corresponding month attached.
Posts: 7,431
Threads: 125
Joined: Sep 2016
Mar-03-2020, 02:08 AM
(This post was last modified: Mar-03-2020, 02:08 AM by snippsat.)
Connect the two list to a dictionary,then can use max() with key parameter.
>>> rainfall = [10, 20, 30]
>>> months = ['January', 'February', 'March']
>>> rainfall_record = dict(zip(months, rainfall))
>>> rainfall_record
{'January': 10, 'February': 20, 'March': 30}
>>>
>>> month, amount = max(rainfall_record.items(), key=lambda x:x[1])
>>> month
'March'
>>> amount
30
Posts: 3
Threads: 1
Joined: Mar 2020
(Mar-03-2020, 02:08 AM)snippsat Wrote: Connect the two list to a dictionary,then can use max() with key parameter.
>>> rainfall = [10, 20, 30]
>>> months = ['January', 'February', 'March']
>>> rainfall_record = dict(zip(months, rainfall))
>>> rainfall_record
{'January': 10, 'February': 20, 'March': 30}
>>>
>>> month, amount = max(rainfall_record.items(), key=lambda x:x[1])
>>> month
'March'
>>> amount
30
THANK YOU! took me a little bit to figure out how to use all of that and what it did but it worked! thank you!
Posts: 1,955
Threads: 8
Joined: Jun 2018
It is good practice to avoid misleading names in code. If it's months rainfall why name it day_rainfall day_rainfall = float(input(f" In {x} what was the rainfall for the month? "))?
Another approach to solve the problem is to find max value index in rainfall list and using this index to find corresponding position in months list. Borrowing Larz60+ data:
>>> rainfall = [1.1, 2.3, .5, 2, 3.1, .25, 1.1, 2.3, .5, 2, 3.1, .25]
>>> months = ['January', 'February', 'March', 'April', 'May',
... 'June','July', 'August', 'September', 'October', 'November', 'December']
>>> f'{months[rainfall.index(max(rainfall))]} has the highest rainfall with {max(rainfall)} inches'
'May has the highest rainfall with 3.1 inches' Note that there are two months with rainfall of 3.1.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|