Nested For Loops Python (Beginner Question) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Nested For Loops Python (Beginner Question)

I have an idea of what I need to do for data extraction from the question, but I am unable to actually write the code from scratch: Daily rainfall (measured in mm) in Brighton for first quarter of the year are stored in the following lists: jan = [1, 3, 0, 4, 6, ...] feb = [5, 0, 0, 2, 3, ...] mar = [10, 31, 21, 53, 9, ...] ... represents more values not shown The following list contains the daily rainfall for the first quarter of the year rainfall = [jan, feb, mar] Write a program to find (a) for each month, the number of days where there is no rain (b) the average rainfall for the first quarter I barely managed (a): for day in jan: if day == 0: print("Zero rainfall days in jan: ") print(day) for day in feb: if day == 0: print("Zero rainfall days in feb: ") print(day) for day in mar: if day ==0: print("Zero rainfall days in mar: ") print(day) How can I 'count' the number of 0's? How can I add all index values and divide by index count for (b)? Thank you in advance!

1st Aug 2020, 9:30 AM
Sean Lim
Sean Lim - avatar
3 Answers
+ 1
Since we shouldn't do the homework for you, I write two other examples to help you. (1) Adding something total=0 for i in [1,2,3]: total=total+i print(total) #total=1+2+3=6 (2) How many numbers are in each list print(len(jan))
1st Aug 2020, 9:40 AM
Panko
Panko - avatar
0
Thanks all! I think I solved (b)...? jan = [1, 3, 0, 4, 6, ...] feb = [5, 0, 0, 2, 3, ...] mar = [10, 31, 21, 53, 9, ...] rainfall = [jan, feb, mar] average_rainfall = total_rainfall / total_days total_rainfall = sum((jan)+(feb)+(mar)) total_days = len((jan)+(feb)+(mar)) print(average_rainfall) Why doesn't this work: average_rainfall = total_rainfall / total_days total_rainfall = sum(rainfall) total_days = len(rainfall) print(average_rainfall) ?
1st Aug 2020, 11:19 AM
Sean Lim
Sean Lim - avatar
0
codemonkey thanks, I'll take note to input my code in my future queries!
1st Aug 2020, 2:14 PM
Sean Lim
Sean Lim - avatar