[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Help me Python /g/
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 14
Thread images: 2
File: BluePic.jpg (2 KB, 266x200) Image search: [Google]
BluePic.jpg
2 KB, 266x200
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 this
for 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.
>>
File: 1466049818840[1].gif (2 MB, 500x281) Image search: [Google]
1466049818840[1].gif
2 MB, 500x281
>>55200441
>>55200015

This all helped quite a bit, thanks actually.

Have a lewd renge
Thread replies: 14
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.