0

I'm a total newbie on Python and I would like to calculate the arithmetic average.

a = [int(i) for i in input().split()]

average=sum(a)/len(a)

print('The average is:' ,average)

I'm aware that such code do solve my problems but that is not exactly what I'm looking for.

I want the user to be able to type the number of terms of the arithmetic average and I would like him to be able of typing them separatley on different lines. So I thought the right thing to use was For Loop. I came out with something like this:

n = input('Number of terms')

for i in range (1,int(n)+1):

    a=input('Term number '+str(int(i))+': ')

I know that all I need to do know is to find a way to sum all values of a typed on each loop and divide this number by int(n) but I have no idea how to do that.

Can you guys help me with that?

Thanks everyone!

3
  • If you are using Python 3, input returns a string. Not an integer (or floating point) number. If you're using Python 2 instead, you shouldn't be using input but raw_input. Commented Jun 13, 2016 at 5:35
  • Since you want to save a sequence of values, it's natural to use some kind of sequence type, what about list? Commented Jun 13, 2016 at 5:35
  • Evert, I'm using Python 3. And Rogalski, I'm not sure how to use lists properly so I tried to solve this with only the very basic knowledge I have. hehe Commented Jun 13, 2016 at 5:42

2 Answers 2

1
n = input('Number of terms')
acc = 0
for i in range(1,int(n)+1):
    a=input('Term number '+str(int(i))+': ')
    acc += float(a)
print('The average is ',acc/int(n))

The idea is to create an accumulator variable acc to which the entered numbers are added. After the loop acc is equal to the sum of all numbers entered. Divide it by the number of terms and you get the arithmetic average.

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

2 Comments

Isn't it word for word the same as my answer?
Oh, I see! I had a problem using this accumulator variable. I'll read more about this. I think the problem has ben solved haha. Thanks :)
0

Try:

n = int(input('Number of terms'))
sum = 0
for i in range (1,n+1):
    a=int(input('Term number '+str(i)+': '))
    sum += a
avg = sum/n

7 Comments

I tried your code but I got this error message: "line 8, in <module> a*=a TypeError: can't multiply sequence by non-int of type 'str'"
a is an int. We explicitly cast it as int in a=int(input('Term number '+str(i)+': ')). Are you sure you cast it?
Yes I did. I just ctrl+c ctrl+v what your answer. I'm using Python 3 btw.
a should be a float to allow the user to enter non-integral numbers.
@SvbZ3r0, what I posted is my entire code! I know it is incomplete but I just didn't know how to proceed. hehe
|

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.