Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

python - How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop.

I need to figure out how to eliminate using 0 as part of the average. example: 5, 0, 5 ,5... the average is 5 by eliminating the 0.

nA = 1
nSum = 0
print ('enter numbers to find the average')
print ('enter 0 to quit.')
while nA !=0:
    nA = input ('gemmi a number:')
    nSum+=nA
   dAvg = nSum

print 'the total amount of numbers is' , nSum ,
print 'your average is' , dAvg , 

How do I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems to me that you need to keep a counter which tells you how many numbers the user has input so you can divide by it to get the average (being careful not to count the final 0). As an aside, the user can never put in 5,0,5,5 here because at the first 0, the loop will break and the other 2 5's won't have an opportunity to be input.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...