Practical lesson: the elif statement
A counter is a variable used to count how many elements have a given property. Its starting value is set to 0, k += 1 is done every time the condition holds, and k is printed at the end. With three numbers a separate if is written for each, because each number must be checked independently; elif would be wrong here. To order two numbers so that the larger is printed first, if…else is enough: if a >= b then print(a, b), otherwise print(b, a). Choosing a name by a number is a usual elif-chain task: for example, for a traffic light 1 – red, 2 – yellow, 3 – green, and for any other number an error message is printed in else. Also test the program with border values (1, 3, 0, 4).
i = 1, 3, 5, 7, 9
Repetitions: 5 · sum = 25
the stop value itself is not included
A PE teacher drew a line on the ground and, for each practice jump, added one to a tally if the jump passed the line. At the end the tally showed the number of successful jumps.
Survey takers, counting staff and programmers use a counter to count elements with a certain property.
Ask ten friends whether they like reading and tally the number of “yes” answers on paper. Do not write anyone’s name.
1a = input().split() 2k = 0 3if int(a[0]) % 2 == 0: 4 k += 1 5if int(a[1]) % 2 == 0: 6 k += 1 7if int(a[2]) % 2 == 0: 8 k += 1 9print(k)
2
1a = int(input()) 2b = int(input()) 3if a >= b: 4 print(a, b) 5else: 6 print(b, a)
8 3
“Counting contest”: teams write a “how many are greater than 5” program for three numbers; the team that first writes a program that works and is correct for all border values wins.