year = int(input("Enter number of years: "))
total = 0
for x in range(year):
for y in range(1,13):
print("Year {0}".format(x+1))
precipitation = int(input("Enter precipitation for month {0} mm: ".format(y)))
total = total + precipitation
totalmonths = year * 12
average = total/totalmonths
print("")
Ok how do i loop it so instead of month 1 , 2 ,3 its January , February etc.
Try something like this:
import datetime
months = [datetime.date(2000, m, 1).strftime('%B') for m in range(1, 13)]
for month in months:
print month
Alternatively, something like this would also work:
import calendar
for i in range(1, 13):
print calendar.month_name[i]
Try this:
# fill a tuple with the month's names
for y in ('January', 'February', etc):
# do something
Alternatively, create a tuple whose indexes map months' numbers to names, starting at index 1
(I'm filling the first position with None
because the index 0
won't be used):
months = (None, 'January', 'February', etc)
for y in range(1, 13):
m = months[y] # `m` holds the month's name
Notice that I'm using tuples and not lists because month names are unlikely to change. And although we could use a dict
that maps month numbers to names, that would be overkill for this simple case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.