【问题标题】:Write a program in python that in a loop asks the user for a integer until the user prints out 0用python编写一个程序,循环询问用户一个整数,直到用户打印出0
【发布时间】:2012-03-11 21:04:23
【问题描述】:

我正在为 Python 考试而学习,但我不知道如何处理这个该死的问题。 用python编写一个程序,循环询问用户一个整数,直到用户打印出0。用户打印出0后,程序将打印出该程序的平均值。

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

【问题讨论】:

标签: python loops integer user-input


【解决方案1】:
from __future__ import division
data = [int(i) for i in iter(raw_input, '0')]
print "mean:", sum(data) / len(data)

【讨论】:

  • 我们真的不应该为人们做功课。但是为 iter(callable, sentinel) 构造 +1 - 不知道这一点。
【解决方案2】:

最好的办法是真正阅读 Python.org 的 WIKI。这是一个很好的信息来源,this link 一定会让您顺利上路。

【讨论】:

  • 哈,非常感谢这个链接。我从来没有遇到过那个网站。
【解决方案3】:

这就是你要找的吗?

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2018-07-15
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    相关资源
    最近更新 更多