Nick
Nick

Reputation: 63

Input looping through a list

This is what I have so far, with proper indentation, but I can't figure how to do it in here. Also the code is incomplete obviously, I am just stuck at the first part.

def main():  
    rains = []  
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']  
    month = 0  
    rain = int(input('Enter the amount of rain for ', months(month)))
    month += 1  
    print(rain)  

main()

I am trying to loop through the months when asking for the rain amounts. So the first would be 'Input for January: ', then it appends the input, then 'Input for February: ', until all the months have been inputted. Thanks.

Upvotes: 0

Views: 56

Answers (1)

Prune
Prune

Reputation: 77837

You're doing fine, except for the loop and a little syntactic sugar:

def main():
    rains = []
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    for month in range(12):
        rain = int(raw_input('Enter the amount of rain for ' + months[month] + ': '))
        print(rain)
        rains.append(rain)
    print rains

main()

Output:

Enter the amount of rain for January: 5
5
Enter the amount of rain for February: 5
5
Enter the amount of rain for March: 5
5
Enter the amount of rain for April: 5
5
Enter the amount of rain for May: 6
6
Enter the amount of rain for June: 8
8
Enter the amount of rain for July: 9
9
Enter the amount of rain for August: 10
10
Enter the amount of rain for September: 16
16
Enter the amount of rain for October: 13
13
Enter the amount of rain for November: 12
12
Enter the amount of rain for December: 10
10
[5, 5, 5, 5, 6, 8, 9, 10, 16, 13, 12, 10]

Upvotes: 4

Related Questions