I'm struggling with nested loops in Python.
So say I want to:
Design a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer-loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.
After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Now, I'm not asking you to do this for me. What I'm struggling with is how to set this up.
From what I understand I need to make 'years' a variable like
years = int(input('Type the number of years:')
and then use a for loop like:
for num in range(years):
rainfall = int(input('Enter the amount of rainfall for this month'))
right? Do I make a list containing each month? What is the nested loop going to look like inside?
pls respond
>>55198942
Read a beginner python tutorial.
>>55198942
>copy and pasting your university questions onto /g/
lmao inb4 youre kicked from your degree
>>55199941
>nested list
>not 2d array
http://greenteapress.com/thinkpython/html/index.html
>>55199938
Not asking for anyone to do it for me, I'm asking if I'm setting this shit up in the right direction.
>>55199954
Thanks
>>55199941
I uh... forgot to escape the code tag
use the code tag for code
Your nested loop would look like thisfor i in range(years):
#code
for j in range(12):
#code for asking rainfall per month
#code for putting data in list
#code
#code
>>55199954
>beginner python
>2d array
kek
>>55200015
>implying 2d arrays are difficult
>>55200015
Thanks fampai
>>55200073
They're certainly more difficult in python compared to nested lists.
>>55200015
Also, using a list for the months would be necessary I'm assuming?
>>55200390
If you want to have specific data for a specific year, a nested list like this[ [m1,m2...m12], #for year 1
[m1,m2...m12], #for year 2
...
[m1,m2...m12] ] #for year n
would be helpful.
Otherwise, you can just store each datapoint in a big list.
>>55200441
>>55200015
This all helped quite a bit, thanks actually.
Have a lewd renge