0

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//////
4
  • ...what?! What output were you expecting, and what do you get instead? Commented Apr 28, 2015 at 16:20
  • I expect to be able to get the accumulative total of all the months gathered in the inner nested loop. It seems like currently the output in the outer loop is coming off of the 12th month's rain input. Commented Apr 28, 2015 at 16:26
  • Well of course it is, you reset the total to zero for each month. Commented Apr 28, 2015 at 16:26
  • I thought I was getting around that via making a total in line 30 rainTotal = rainTotal + monthlyRainFall Commented Apr 28, 2015 at 16:33

1 Answer 1

1

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):

Sign up to request clarification or add additional context in comments.

3 Comments

That fixed it. Jeez. So really what I failed at here was the scope of my variables then. Sigh. Thank you very much JBiss. BTW I would think declaring the rainTotal variable in the outer loop would make the scope of that variable a local variable only inside that outer loop versus for both inner and outer?
On every iteration of the inner loop you were re-defining rainTotal - hence it was losing its value.
Oh wow that's good to know I thought if it was a definition without a value say of 0 or 1 etc it wouldn't do that. Thank you for the enlightenment!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.