Practical lesson: simple problems
In this lesson we work independently: first write each task in four stages in the notebook (input data, result, calculation, test), and only then write code. In geometry tasks write the formula correctly once and declare PI once as a constant. The area of a ring is the area of the big circle minus the area of the small one: PI * (R ** 2 - r ** 2); if the brackets are forgotten the answer is different. The sum of the numbers from 1 to n can be found without a loop by the formula n * (n + 1) // 2. If the program gives a wrong answer, check the formula first and then the variable names; a misspelled variable name gives a NameError. In all tasks make it a habit to check the answer by hand.
A class wanted to cover a wall display board with paper. A pupil worked in four stages: the board’s width and height were the data, the area was the result, the formula was width times height, and the answer was tested against a rough estimate.
Builders, gardeners and programmers use measurements to calculate and check a needed quantity.
Take the width and length of your room or classroom. Find the area on paper with a formula and a calculation.
1n = int(input()) 2print(n * (n + 1) // 2)
5050
1PI = 3.14 2a = input().split() 3R = int(a[0]) 4r = int(a[1]) 5print(PI * (R ** 2 - r ** 2))
50.24
“Formula workshop”: the group writes on cards the formulas for the area of three shapes (a circle, a ring, a right triangle) in Python notation; a partner gives values and calculates by hand and by program.