简体   繁体   中英

Write a program in python that in a loop asks the user for a integer until the user prints out 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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM