Python Forum
Using lists, can't seem to get a total...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using lists, can't seem to get a total...
#1
Hello Everyone, I am learning how to make and use lists in python. I have a simple program made and I am struggling at the point when trying to get a sum of all the "user inputs" that are stored in my list. Can someone drop me a hint? I'd like to get a total and a sum of my user inputs as seen in the following code.
# This program will ask a user to enter rainfall data for
# the twelve months in a year. It will take the user’s input
# and return the rainfall total, and the annual average.

# Declare constant integer
SIZE = 12

def main():
    # Create a list to hold the values of rainfall each month
    # as given by the user
    monthlyRain=[SIZE]
    # Create a variable to use as the counter
    months=0
    # Ask the user to input data for 12 months of rainfall
    enterRain()
    # Calculate and display the total and average rainfall
    rainStats(monthlyRain)

# The enterRain module will ask the user for 12 months of rainfall data
# and store the data in a list called monthlyRain
def enterRain():
	for months in range(SIZE):
		print(int(input("Please enter rainfall for month " + \
		str(months+1) + ":")))
		months += 1
                                    
                                      
# The rainStats module will calculate the 
# total rain for the year and the 
# overall average. 
def rainStats(monthlyRain):
	total=0
	for num in monthlyRain:
		total += num
		print("The total rainfall for the year is: " , total, "inches")
		average = total/12
		print("The average rainfall for the year is: " , average, "inches")
main()    
    
        
    
Reply
#2
Too much documentation is worse than none.
Try using """""" immediately after function def, example:
def main():
    """
    Create a list to hold the values of rainfall each month as given by the user

    :return: None
    """
    monthlyRain = enterRain()
    ...
Now if the module were named RainList.py,
It can be imported into an interattive session, and you will be able to get documentation like:
λ python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import RainList
>>> help('RainList')
Help on module RainList:

NAME
    RainList

FUNCTIONS
    main()
        Create a list to hold the values of rainfall each month as given by the user
        :return: None

FILE
    x:\python\forum\src\rainlist.py
>>>
Now for your application.
Since you are not using a class, functions need to be defined before being used (need to be interpreted so they can be called)
Also, variables declared in one function are not visible to another function, so you need to pass and return values, for example in
your code:
def enter_rain(month):
   return int(input('Get rain for month {}: '.format(month)))

def main():
    months = 12
    monthly_rain = []
    for n in range(months):
        monthly_rain.append(enter_rain(n+1))
# etc.
Reply
#3
OK working on this, thanks for the tips, I can see how I missed my "returns".
Reply
#4
Note the update to my post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020