Computing the average of non-negative numbers from a text file
<p>I am trying to read in a text file of integers, make it a list, compute the average of all integers, compute the average of all non-negative integers, print max and min. I was able to compute the average of all integers but am having difficulty getting the average of all non-negative integers and the max and min. </p>

<p>Here is what I have so far:</p>

<pre><code>file = open("numberlist.txt", "r")

sum = 0

list = []
for num in file:
    list.append(num)

poslist = []
for number in file:
    x = int(number)
    if x &gt; 0:
        poslist.append(x)
        sum += number
posavg = sum / len(poslist)
print("The number of non-negative integers is ", len(poslist))
print("The average of the non-negtive integers is ", posavg)
</code></pre>

<p>This keeps most of your code and adds the non-negative part to it (perhaps should be called positive instead? :)</p>

<pre><code>file = open("numberlist.txt", "r")

sum = 0
nonNegativeTotal = 0
nonNegativeCount = 0

list = []
for num in file:
    list.append(num)

for number in list:
    x = int(number)
    if x &gt;= 0:
        nonNegativeCount += 1
        nonNegativeTotal += x
    sum += x
avg = sum/len(list)
avgNonNegative = nonNegativeTotal/nonNegativeCount
print("The number of integers is ", len(list))
print("The overall average is ", avg)
print("The number of non-negative numbers is ", nonNegativeCount)
print("The non-negative average is ", avgNonNegative)

list.sort()
print("The minimum number is ", list[0])
print("The maximum number is ", list[-1])
</code></pre>

<p>For the min and max you could do:</p>

<pre><code>minNum, maxNum = min(list), max(list)
</code></pre>

python python-3.x if-statement for-loop accumulator