2

I have following code, that will ask user each month's rainfall, and append that to rainfall_inch list, and calculate average but i have two issues

1) after the user enters a number and next input runs, the previous entry shows behind the next input.

"Enter rainfallJanuary  :  3
Enter rainfallFebruary 3 :  4"

and so on
2) problem is "'list' object cannot be interpreted as an integer" with my code.

def main():

    rainfall_inch = []

    name_month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]


    for item in name_month:
        x = input()
        print("Enter rainfall" + item, x, ": ", end=' ')
        rainfall_inch.append(x)
    b = [float(x) for x in range(rainfall_inch)]


    print(b)

    print("average rainfall is:", computeAverage(b))

def computeAverage(values):
    #find average of rainfull
    total = 0.0
    for x in values:
        total+=x
    average = total/len(values)
    return average

main()

help thank you

4
  • change x = input() to x = int(input()) Commented Nov 9, 2018 at 4:57
  • when running your code, I get a SyntaxError at end=" " - I hence can't reproduce your error Commented Nov 9, 2018 at 5:04
  • @sudonym You are running in Python 2.x, wheras the code is in 3.x. Commented Nov 9, 2018 at 5:06
  • I understand - this should IMO be highlighted somewhere Commented Nov 9, 2018 at 5:07

2 Answers 2

1

Here, I've fixed some logic errors with your code, Look for comments so as to see where,

def main():

    rainfall_inch = []

    name_month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

    for item in name_month:
        print("Enter rainfall" + item, ": ", end=' ')
        x = input()
        if x == '':
            x = 0

        rainfall_inch.append(x) # you are appending here
    b = [float(i) for i in rainfall_inch] # so remove the range here, its wrong
    # range generates integers from a to b where a and b are integers
    print(b)

    print("average rainfall is:", computeAverage(b))

def computeAverage(values):
    #find average of rainfull
    total = 0.0
    for x in values:
        total+=x
    average = total/len(values)
    return average

main()

Outputs:

Enter rainfallJanuary :  40
Enter rainfallFebruary :  50
Enter rainfallMarch :  30
Enter rainfallApril :  60
Enter rainfallMay :  50
Enter rainfallJune :  30
Enter rainfallJuly :  330
Enter rainfallAugust :  20
Enter rainfallSeptember :  50
Enter rainfallOctober :  90
Enter rainfallNovember :  30
Enter rainfallDecember :  430
[40.0, 50.0, 30.0, 60.0, 50.0, 30.0, 330.0, 20.0, 50.0, 90.0, 30.0, 430.0]
average rainfall is: 100.83333333333333
Sign up to request clarification or add additional context in comments.

3 Comments

somehow when i run it i still have error saying "could not convert string to float:"
also why it has "rainfallNovember 20 : 25,"that "20" before ":"
@RainyDay The problems are fixed now, Try running it.
0

I have removed range() like this

b = [float(x) for x in rainfall_inch]

and found to be working without error

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.