☰ Contents · Computing

Simple task practice and logical expressions

Lessons 34–35 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
34

Practical lesson: simple problems

Textbook: p. 72
GoalIn the practical lesson, programs simple tasks by the four stages: chooses the variables, writes the formula and tests the result.
New words
formula: a rule written with variables, for example S = PI * r ** 2 · formulaconstant PI: written in capital letters; here PI = 3.14 · doimiy PItest: a run with a known answer to check the program · sinovtype of the result: int or float, depending on the task · natija turi
Explanation

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.

Worked examples
The sum from 1 to n: n = int(input()) ⏎ print(n * (n + 1) // 2) If 100 is typed, 5050 is printed; to check by hand take n = 4: 1 + 2 + 3 + 4 = 10.
Area of a ring: PI = 3.14 ⏎ a = input().split() ⏎ R = int(a[0]) ⏎ r = int(a[1]) ⏎ print(PI * (R ** 2 - r ** 2)) With R = 5 and r = 3 the area is 3.14 · (25 − 9) = 50.24.
Class activity

“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.

Practice
1
7350 g of flour was brought to the kitchen. Write it in kilograms and grams (using // and %).
2
The air temperature on three days was 18, 21 and 24 degrees. Find the average temperature.
3
Each truck carries 30 sacks. How many trucks are needed to carry 250 sacks?
4
Why must R ** 2 - r ** 2 be put in brackets in the area of a ring?