I have to create a program in python that inputs numbers from the keyboard until the user types -99. Then, the program will output the average of the numbers, rounded to one decimal place. This is an example-. This is what I did so far, Click on this link
while number != -99: number = int(input("")) sum = sum + number count += 1
The problem is that you count -99 also to the sum and the count. this will drastically change your output. Since the code should not divide (1+4+5+7+8+ -99)/(count = 6) but (1+4+5+7+8)/(count = 5).
So this means if you use the number -99 as the number to step out of the loop you cant count it as a valid input to the sum and the count.
Also your count starts on zero. This means the number which you divide the sum with will be 1 less than the actual number.
You could define this as 1 or you could remove the -1 in
(count-1) when you calculate the answer.
If your stuck, here is a working version. Try yourself first tho.
count = 1 sum = 0.0 number = 1 if count == 0: print("Input some number") while number != -99: number = int(input("")) if(number != -99): sum = sum + number count += 1 print("the average should be: ",float(sum / (count-1)))
Your program works for the first 2 examples however in the third program, when I input 1, 4, 5, 7 , -99, the output shows 4.25. Its supposed to output 4.2.
I bet you can handle that part yourself.
You will need to round or floor the number. This is a good place to start your search.
Can you help me out, I still can’t figure it out, Is it
Sorry mate. It is a learning experience. Go by trial and error. If you fail you’le just have to start again.
“give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime”