Search code examples
for-loopglobal-variablespass-by-referencenested-loopspython-3.3

Inability to pass values in variable between inner and outer for loops python 3.3


I'm attempting utilizing TeamTreehouse learning subscription & this Starting Out With Programming Logic And Design book to attempt learning programming & python. Please don't shoot to kill me I'm having difficulty with repetition structures!

Goal: I'm attempting to collect input from a user in the outer for loop. The inner loop will iterate 12 times per outer loop iteration calculating; getting for rainfall of each month. The outer loop will then; display the number of months, total inches of rainfall and the average rainfall per month for the whole time period (1 or 7 etc years).

I'm reading on passing values by reference or by value to find that python has mutable and immutable data types (which int is an immutable data type) so I can't simply pass the data between for loops from my understanding. How then do I get this to function? I had a list suggested to me though I don't understand how to get an average out of a list because frankly it hasn't been covered on teamTreehouse or in chapter 4 of my book so far. http://en.wikibooks.org/wiki/Python_Programming/Data_Types

Error: Inability to get data transferred from internal nested loop variable rainTotal to the outer loop rainTotal.

CODE:

#//////MAIN PROGRAM START//////

#//////VARIABLE DECLARATION//////
totalMonths=0
rainAverage=0
rainFall=0
rainTotal=0
#//////VARIABLE DECLARATION//////

#//////USER INPUT FUNCTION//////
def userInput():
    years=0
    months=12
#////don't understand how to function properly
#    monthly_rain = []
#////don't understand how to function properly
    print('This program will calculate the average rainfall over a period of years.')
    years=int(input("Please provide the number of years to calculate rainfall for."))
    for i in range(1, years + 1):
    #////////////////testing variable values correct////////////////
    #Placeholder
    #////////////////testing variable values correct////////////////
#//////USER INPUT FUNCTION//////
        for i in range(1, months + 1):
            rainTotal=int()
            monthlyRainFall=int(input("Please provide the rainfall in inches for month number " + str(i) + str(": ")))
#////don't understand how to function properly
#            monthly_rain.append(monthlyRainFall)
#////don't understand how to function properly
            rainTotal = rainTotal + monthlyRainFall
            rainAverage=rainTotal/months
            #//////testing variable <> value assignment/////
#///////// python code references/////////////
#            print('Calculating for a total number of', totalMonths, 'months.')
#            print('Months\t\t\t' + 'Average Rainfall')        
#            print(rain, '\t\t\t\t\t', i)
#/////////format references/////////////
    print("There was a total of ", (years*months), "months calculated.")
    print("The accumulative total of rainfall was ", rainTotal, " inches!")
    print("Average Rainfall per month:", rainTotal/(years*months))
# after the inner loop runs the following should display

#//////CALLING FUNCTION//////
userInput()
#//////CALLING FUNCTION//////

Solution

  • As said - please expand on what error you are receiving. But from looking at your code, try defining rainTotal before you enter the inner loop. i.e:

    for i in range(1, years + 1): rainTotal=int() #here for i in range(1, months + 1):