Lessons 32–33 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
32
Practical lesson: input and output
Textbook: pp. 69–70
GoalIn the practical lesson, programs tasks on dividing quantities equally and splitting them into units with // and %, and reads values from one line with split().
New words
equal sharing: // gives each share, % gives what is left over · teng taqsimlashremainder: the part that does not make a whole share · qoldiqarithmetic mean: the sum of the numbers divided by how many there are · o‘rta arifmetiksplit(): cuts a typed line into parts · split()
Explanation
Most tasks of this lesson are alike: an amount is shared into equal portions and the question is what is left over. Here // gives each portion or the number of whole groups, and % gives the leftover part. In a money example we find the number of the biggest banknotes with //, and then repeat the same thing for the remaining sum with smaller banknotes. If several numbers are typed in one line, input().split() separates them, and then each part must be converted with int(). To find the arithmetic mean of several numbers, put the sum in brackets and divide by how many numbers there are. Always test the program on 2–3 examples worked out by hand, especially in the case where the remainder is zero.
Worked examples
Splitting money into banknotes: n = int(input()) ⏎ a = n // 50000 ⏎ r = n % 50000 ⏎ b = r // 10000 ⏎ c = r % 10000 ⏎ print(a, b, c) If 187000 so‘m is typed, the result is 3 notes of 50000, 3 notes of 10000 and 7000 left (3 3 7000).
Putting pencils into boxes: a = input().split() ⏎ k = int(a[0]) ⏎ m = int(a[1]) ⏎ print(k // m, k % m) With 53 pencils and 12 per box, there are 4 full boxes and 5 pencils left (4 5).
Class activity
“Shop cashier”: a pair chooses the price of an item and the money paid; it writes a program giving the change in the largest banknotes (// and %), and the partner checks it by hand.
Practice
1
How many full 50000 so‘m notes are in 187000 so‘m?
3
2
We pack 53 pencils 12 per box. How many boxes are needed to hold all of them?
5 (4 full boxes and one more for the rest)
3
Write a program that prints the arithmetic mean of three numbers typed with spaces.