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

2024/11/16 11:43:06

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+=nAdAvg = nSumprint 'the total amount of numbers is' , nSum ,
print 'your average is' , dAvg , 

How do I do this?

Answer

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.

https://en.xdnf.cn/q/118931.html

Related Q&A

NoSuchElementException when loading code using Selenium on Heroku

Error: ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name=Task-30 coro=<Dispatcher._process_polling_updates() done, define…

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…

Python 3 - exec() Vs eval() - Expression evaluation [duplicate]

This question already has answers here:Whats the difference between eval, exec, and compile?(3 answers)Closed 7 years ago.After reading query.below python code is still not clear,>>> exec(pri…

What is the syntax for printing multiple data types in Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

When scraping all the div to get the data getting the null list using lxml in python

I want to scrape the product title , product link , product price but when I am using the xpath it is showing the null list . How to add the xpath and for loop to get the above details . I have tried …

Python how to convert this for loop into a while loop [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Converting a for loop to a while loop I have this for a for loop which I made I was wondering how I would write so it woul…

Joining elements in Python list

Given a string, say s=135 and a list, say A=[1,2,3,4,5,6,7], how can I separate the values in the list that are also in s (a digit of s) from the other elements and concatenate these other elements. Th…

Python - Count letters in random strings

I have a bunch of integers which are allocated values using the random module, then converted to letters depending on their position of the alphabet.I then combine a random sample of these variables in…

Looping until a specific key is pressed [duplicate]

This question already has answers here:detect key press in python, where each iteration can take more than a couple of seconds?(4 answers)Closed 2 years ago.I was trying to make a while loop which wou…

Getting sub-dataframe after sorting and groupby

I have a dataframe dfas:Election Year Votes Vote % Party Region 0 2000 42289 29.40 Janata Dal (United) A 1 2000 27618 19.20 Rashtriya Ja…