## Midterm Exam 2 Review Fall 2003: Rainfall problem

(Back to Fall2003 Midterm Review 2)

I think this is part A
```def rainfall(list):
count=0
newlist=[]
#count how many are in the list
for item in list:
if item >= 0:
count=count+1
#now add all the positive numbers to a new list
for item in list:
if item >= 0:
newlist= newlist + [item]
print "this is the newlist", newlist
print "this is how many items are in the list",count
#now add the items in the list together
total=0
for item in newlist:
total=total+item
print "this is the total in the list", total
```

 You can do it without three separate loops. Mark Guzdial

same old error of doesn't exist again at line "negativeNumber"

```def rainfall(list):
openREMOVEDst = open(list,"rt")
openREMOVEDst.close()
i = 0
sum = 0
for item in listContent:
negativeNumber = listContent.rfind("-",0,item)
print negativeNumber
if negativeNumber <> -1:
i = i + 1
sum = sum + int([item])
print sum / i
```

 You can assume that you have a list of numbers, not strings. Mark Guzdial

is there something wrong with the first one?

modified, so 999 would not be included
```def rainfile(nlist):
sum = 0
count = 0

for entry in nlist:
if entry >= 0:
if entry == 999:
break
sum = sum + entry
count = count + 1

print (sum/count)
```

houman

```def rainfall(list):
#check for 999 in the list
indexcount = 0
isFound = 0
for number in list:
if number == 999:
if isFound <> 1:
check = indexcount
isFound = 1
else:
indexcount = indexcount + 1
if isFound == 0:
check = -1
total = 0
counter = 0
if check == -1:
for number in list:
if number >= 0:
total = total + number
counter = counter + 1
if check <> -1:
for number in range(0, check + 1):
if list[number] >= 0:
total = total + list[number]
counter = counter + 1
print total
average = total / counter
print average
```

and that is the extra credit portion of the problem without using "break" .. houman's looks much nicer though lol

Its really hard to read that code without proper spacing, try to use the html protocols to correctly space it out. You can find all of them on the FAQ page! Brittany Selden

Unfortunatly houman's code includes the 999 in the average returning an average of 211 instead of 14. While it is not stated in the problem 999 doesn't seem like it should be included in the average. Try this code.

```def rainfallavarage(string):
total = 0
number = 0
average=0
for i in string:
if i==999:
average=total/number
print(average)
break
if i>=0:
total=total+i
number=number+1
```

Student549

well the problem says "...if the number 999 appears in the list, add in no later numbers in the list. So, if the above example were input, the 17 would not be added into the average."

I won't argue with that, maybe a TA or Mr. Guzdial can clear this up.
Student549

I just modified one of the codes and thats what i got. It works.
def rainfall(list):
count=0
newlist=[]
total=0
#count how many are in the list
for item in list:
if item==999:
break
if item >= 0:
count=count+1
newlist= newlist + [item]
for item in newlist:
total=total+item
print"the average is", (total/count)

 Right, the idea is that once you see 999, stop. Don't read any REMOVED numbers. Mark Guzdial