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

Help me Python /g/

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 2

File: BluePic.jpg (2KB, 266x200px) Image search: [Google]
BluePic.jpg
2KB, 266x200px
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 (2MB, 500x281px) Image search: [Google]
1466049818840[1].gif
2MB, 500x281px
>>55200441
>>55200015

This all helped quite a bit, thanks actually.

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


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.