Continuously ask the user to enter numbers until the user enters a number that is greater than 100. Then print the average value of the numbers before the last input.
Here is what i have so far
def main():
sum = 0.0
coum = 0
num = input("Enter a Number")
while num <= 100:
sum = sum + num
coum = count
num = input("Enter a Number")
ave = sum/count
print ave
To test this, you should think of cases which you can walk through your code and see if the requirements are satisfied. Start with the simplest case in the beginning and work your way up to more complex cases.
Consider the case where the first number is greater than 100 at the start. What lines get executed?
The calculation of
aveshould be done outside of the loop. But even then, you still have to worry about the division by zero problem ascoumwould be zero. I’ll leave that for you to think about. Let’s assume that for the rest of this walkthrough, that calculation is moved out of the loop.Next consider the case where the numbers are entered in this order:
100,200. What lines get executed this time?There is no
countvariable here. So you can’t assign some unknown variable to another. Though since we’re trying to calculate the average of the numbers, a crucial number we need is the “count” of numbers that we are averaging. Thecoumvariable really should have beencount. At this point, you would want to be incrementingcountby1. Figure out how to do that. Let’s continue.Try the same thing for the numbers:
100,50,200. What lines get executed then? Figure that out and see if it still satisfies your requirements. If it does, then great, try the next case. If not, find out what’s wrong and try to fix it. If you’re stumped, ask about it. I hope you do a better job at that next time you ask a question here.