0

Im studying for a exam in python and I dont know what to do with this freaking question. Write a program in python that in a loop asks the user for a integer until the user prints out 0. After the user prints out 0 the program shall print out the mean value of this program.

Pardon my probably not so well written English.

2
  • 1
    What have you tried? Commented Feb 21, 2012 at 14:46
  • 1
    @Josh Damn, you beat me by 2 seconds Commented Feb 21, 2012 at 14:47

3 Answers 3

5
from __future__ import division
data = [int(i) for i in iter(raw_input, '0')]
print "mean:", sum(data) / len(data)
Sign up to request clarification or add additional context in comments.

1 Comment

We shouldn't really be doing people's homework for them. But +1 for the iter(callable, sentinel) construct - didn't know about that.
2

Your best bet is to really read through Python.org's WIKI. It's a great source of information and this link will surely get you on your way.

1 Comment

Ha, thanks a lot for that link. I've never come across that website.
0

Is this what you are looking for?

x = []
i = ''
while i != 0:
    i = int(raw_input("Please enter an integer: "))
    x.append(i)

sum = 0
for number in x:
    sum = sum + int(number)

print float(sum/int(len(x)-1))

Comments

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.