-1

So I have written up parts of a program that says I can take in any number of positive numbers and zero, and to exit the program I can write a negative number. I now want to print out the average of this. How would I print out the average?

The bits so far.

i = int(eval(input('Enter a number positive number. Enter a negative number to exit: ')))
while i >= 0:
    i = int(eval(input('Enter a number positive number. Enter a negative number to exit: ')))
Kagamine Len
  • 121
  • 4
  • 1
    instead of `i=` try `i+=` inside the loop. also add a `j` counter that increments every time a non negative number is added. at the end print i/j (which is the average) – Nullman Jun 24 '17 at 16:28
  • 10
    Please, for the love of god and everything that is holy, [remove the `eval`](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice). – Aran-Fey Jun 24 '17 at 16:29
  • 9
    Why are you using both `eval()` *and* `int()`? Use one or the other, as long as it is not `eval()`. – Martijn Pieters Jun 24 '17 at 16:31
  • 2
    When solving a problem like this, the first step is to turn off your computer. Then get a piece of paper and pencil and describe **in words** the steps needed to solve it. – Code-Apprentice Jun 24 '17 at 19:02

1 Answers1

1

how about something like this?

j = 0
total = 0
while True: #"infinite" loop
    i = int(input('Enter a number positive number. Enter a negative number to exit:'))
    if i < 0: #loop escape clause
        break
    total += i
    j+=1
if j > 0: #avoid division by zero
    average = total/j #rounded average
else:
    average = 0
Nullman
  • 4,179
  • 2
  • 14
  • 30
  • Upon testing the code, I don't see the average when I hit a negative number. When I enter a negative number, I want to be able to see the average. – Kagamine Len Jun 24 '17 at 16:54
  • @PM2Ring you are correct, i forgot sum is a builtin. i edited my answer – Nullman Jun 28 '17 at 19:21