Nov-10-2020, 04:24 AM
Hello, I am new to Python and need help with my program. I need to create a program that assigns random integers in between 0 and 4 to each month of the year. Next I need to calculate the sum and average. Then I need to display all iterations of the lowest rainfall months and the highest rainfall months, which is where I'm struggling. I specifically need help with my function called find_minmax. Does anyone know where I'm going wrong? My program:
import random #define monthly rainfall function def monthlyRainfall(): #assign diff random numbers numbers to each month between the values of zero and four rainfall_jan=random.randint(0, 4) rainfall_feb=random.randint(0, 4) rainfall_mar=random.randint(0, 4) rainfall_apr=random.randint(0, 4) rainfall_may=random.randint(0, 4) rainfall_jun=random.randint(0, 4) rainfall_jul=random.randint(0, 4) rainfall_aug=random.randint(0, 4) rainfall_sep=random.randint(0, 4) rainfall_oct=random.randint(0, 4) rainfall_nov=random.randint(0, 4) rainfall_dec=random.randint(0, 4) #create two dimensional list for the months and rainfall monthly_rainfall = [ ['January',rainfall_jan], ['February', rainfall_feb], ['March', rainfall_mar], ['April', rainfall_apr], ['May', rainfall_may], ['June', rainfall_jun], ['July', rainfall_jul], ['August', rainfall_aug], ['September', rainfall_sep], ['October', rainfall_oct], ['November', rainfall_nov], ['December', rainfall_dec] ] #calculate the total amount of rainfall total=(rainfall_jan+rainfall_feb+ rainfall_mar+rainfall_apr+ rainfall_may+rainfall_jun+ rainfall_jul+rainfall_aug+ rainfall_sep+rainfall_oct+ rainfall_nov+rainfall_dec) #calculate average rainfall average=total / 12 #calculate the minimum and maximum rainfall #print all iterations of month only def find_minmax(): lowest = min(monthly_rainfall, key=lambda x: (x[1],x[0])) highest = max(monthly_rainfall, key=lambda x: (x[1],x[0])) minList = [] maxList = [] strMin = '' strMax = '' for row in range(len(monthly_rainfall)): for col in range(len(monthly_rainfall[row])): if col==1 and monthly_rainfall [row][col]==lowest: minList.append(monthly_rainfall[row][0]) elif col==1 and monthly_rainfall[row][col]==highest: maxList.append(monthly_rainfall[row][0]) strMin = ', '.join(minList) strMax = ', '.join(maxList) print('Lowest rainfall: ', strMin) print('Highest rainfall: ', strMax) #display ascending rainfall by month def ascend(): monthly_rainfall.sort(key=lambda x:(x[1])) for ascend in monthly_rainfall: print(*ascend) #print the two dimensional list print(monthly_rainfall) #print the table function table() #print the two dimensional list with each nested list on a new line for row in monthly_rainfall: for cell in row: print(cell, end=' ') print() #print horizontal dashes divider() #print the total rainfall print('Total rainfall:', total) #print the average rainfall and format number to two decimal places print('Average rainfall:', format(average, ',.2f')) #call the min/max function find_minmax() divider() #call ascend function print('Months sorted by rainfall') print('Ordered by calendar month') divider() ascend() #create the divider (horizontal dashes) def divider(): print('----------------') #create table function def table(): print() print('Month | Rainfall') divider() def main(): monthlyRainfall() main()