Midterm Exam 2 Review Spring 2004: Rainfall problem
Comments, answers, and questions go here:
(Link back to Sp2004 Midterm 2 Review)
On this problem, do we use the specific example list given in the problem or do we write a function that can use a generic list? Lindsey Richardson
| Generic – should work for any list of integers. Mark Guzdial |
Ok...my code will allow for any random list of pos and neg numbers, but now, and print the pos numbers, but now how do I get rid of numbers after 999. I saw a method in the book called remove(something). Is there any way to use this, or am I totally off base? Lindsey Richardson
As to the previous question, I don't think I really understand dot notation. How do you use it? Can you use it in code or just in the command area of JES?
| Both in program and command areas. Lindsey, you might also look up "break" – we'll introduce it next week in breakout. Mark Guzdial |
Ok...so I cant even get the simple one to work. what is going on here?
Amelia Cipolla
def rainfall(string):
#mylist=[string]
total=0
count=0
if i in string > -1:
total=total+[i]
count=count+1
average=int(total/count)
print average
def rainfall(string):
total=0
count=0
for i in string:
if i in string > -1:
total=total + i
count=count+1
average=(total/count)
print average
This printed:
1
2
2
2
3
Is that what you are looking for?
for some reason when you set a parameter to not include negative numbers like above, the program ignores it and includes them in the aversage anyways... how do you fix that??
| I've never seen if i in string > -1! I'm surprised that it doesn't generate an error! What do you think it's doing? Mark Guzdial |
I forgot to post mylist= ["1,3,2,4,5"] Those are the numbers I used and when I put rainfall(mylist) in the command area it gave the average of each time you added in a new number.
list=[12, 0, 41, -3, 5, -1, 999, 17]
def rainfall(list):
totalRainfall = 0
inputs = 0
for i in list:
if i >= 0:
totalRainfall = totalRainfall + i
validInputs = validInputs + 1
print totalRainfall/inputs
Shelley Moister
these are all still including the negative numbers in their average, why is that????
here is my code, but it isn't working, i get this error:
TypeError: __add__ nor __radd__ defined for these operands
Please check line 8 of C:\Documents and Settings\Kenny\My Documents\MidTermExam2\rainfall.py
Where am i going wrong?
def rainfall(string):
positives = 0
numbers = 0
for x in string:
if x>=0:
if x ==999:
break
positives = positives + x
numbers = numbers + 1
print(positives/numbers)
I put what we did during the review last night in JES, and I am finding an error on line 5. Why?
def rainfall(listofrains):
positives=0
numberofpositives=0
for x in listofrains:
if x>=0
if x==000:
break
positives=positives+x
numberofpositives=numberofpositives+1
print positives/numberofpositives
The error I am getting is this: Your code contains at least one syntax error, meaning it is not legal jython.
The error is on line 5
poof #2
NEVER MIND! I got it figured out... I made a couple of mistakes in that code, so please ignore it! Actually, I am now getting my mistake in line 6. Why?
def rainfall(listofrains):
positives=0
numberofpositives=0
for x in listofrains:
if x>=0:
if x==999:
break
positives=positives+x
numberofpositives=numberofpositives+1
print positives/numberofpositives
Sorry... here we go again!
def rainfall(listofrains):
positives=0
numberofpositives=0
for x in listofrains:
if x>=0:
if x==999:
break
positives=positives+x
numberofpositives=numberofpositives+1
print positives/numberofpositives
poof #2
There is still a mistake in line 6 and this is what it says: >> Your code contains at least one syntax error, meaning it is not legal jython.
The error is on line 6
something is up with the positives = positives + x, no one is getting it right, how are we to add all the numbers together?
| Lauren, try something like if x>=0 and x==99. I did this problem in breakout today, so somebody from that section could post the solution we did. Mark Guzdial |
Mr. Guzdial, will we have to know what break means? I have a general sense, but could you explain it in a general sense? As in, is it always by itself and does the if statement above have to have two equal signs? Thanks!
Everyone in that breakout session ~ please post the code! Thx.
I think up there the student meant explain in more specifically: like, the purpose and when break should/must be used.
Is 999 supposed to be included in the list?
It would seem almost as insane as having negative amounts of rain, to get 999 inches, so are we to exempt 999 and everything after or just everything after? Yo, breakout, how about the solution. I got one george washington with your name one it.
Thomas Sobeck
i think you should have positive=positive + x under the first if statement, but I could be wrong. Blank
def rainfall(listofrains):
positives=0
numberofpositives=0
for x in listofrains:
if x>=0:
positives=positives+x
if x==999:
break
numberofpositives=numberofpositives+1
print positives/numberofpositives
it works, but it divides the positive numbers by the total number of items in a list, and dont we want it to only divide by the number of positive numbers? How would this happen in the code? Blank
ok, so i answered my own question. and here it is:
def rainfall(listofrains):
positives=0
numberofpositives=0
for x in listofrains:
if x>=0:
positives=positives+x
numberofpositives=numberofpositives+1
if x==999:
break
print positives/numberofpositives
Blank
Link to this Page