2012-02-21 68 views
0

林Python中考试学习,我不知道如何处理这个问题,再用做。 在python中编写一个程序,在循环中询问用户一个整数,直到用户输出0为止。用户输出0后,程序应打印出该程序的平均值。编写一个程序,在一个循环中请求一个整数用户到用户打印出0

请原谅我的英文写得不好。

+1

[你尝试过什么?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Josh 2012-02-21 14:46:36

+0

@OcasoProtal我喜欢它。 – Josh 2012-02-21 14:47:18

+1

@乔希该死的,你打了我2秒 – 2012-02-21 14:47:50

回答

2

最好的选择是真正阅读Python.org的WIKI。这是一个很好的信息来源,this link肯定会让你顺利。

+0

哈,非常感谢那个链接。我从来没有遇到过这个网站。 – 2012-02-21 15:02:31

5
from __future__ import division 
data = [int(i) for i in iter(raw_input, '0')] 
print "mean:", sum(data)/len(data) 
+0

我们不应该为他们做人的功课。但是'iter(callable,sentinel)'结构的+1不知道。 – 2012-02-21 15:47:44

+0

是的,非常酷。我学到了一些新东西。 – Carlos 2012-02-21 18:17:16

0

这是你在找什么?

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)) 
相关问题