The average of three numbers
The average of three numbers is their sum divided by 3. The pattern is the same as for any average: add everything, then divide by how many. The brackets matter. Python does division before addition, so a + b + c / 3 divides only c. With 50, 60 and 70 the right line gives 60.0 and the wrong one gives 133.33333333333334. Notice also that the answer is 60.0 and not 60: the sign / always gives a decimal result, even when the division comes out exactly. The sign // gives the whole-number part instead: 7 / 2 is 3.5 but 7 // 2 is 3. Some books say that you must use float for an average, but the input numbers here are read with int and the average is still right, because / does the job.
1a = int(input("First mark: ")) 2b = int(input("Second mark: ")) 3c = int(input("Third mark: ")) 4avg = (a + b + c) / 3 5print(avg)
First mark: 50 Second mark: 60 Third mark: 70 60.0
1a = 50 2b = 60 3c = 70 4print((a + b + c) / 3) 5print(a + b + c / 3)
60.0 133.33333333333334
1print(7 / 2) 2print(7 // 2) 3print((50 + 60 + 70) // 3)
3.5 3 60
1a = int(input("First mark: ")) 2b = int(input("Second mark: ")) 3c = int(input("Third mark: ")) 4avg = (a + b + c) / 3 5print(avg)
First mark: 40 Second mark: 70 Third mark: 100 70.0
Plan a program that finds the average of the four quiz marks of an invented pupil. Write the four skills, the formula with brackets, and test it with marks 8, 6, 10 and 8. Then change the line without brackets and see by hand what is wrong.