Mini tasks: temperature and a quiz

Lesson 11 of 26 · Basic Python

In this lesson you will practise branching in Python through practical examples and mini tasks. The goal is to use if, elif and else together with logical operators.


1. Checking the temperature


Write a program that checks the temperature and prints a matching message:



temperature = float(input("Enter the temperature in °C: "))

if temperature < 0:
    print("It is below zero")
elif temperature <= 20:
    print("It is cold")
elif temperature <= 30:
    print("It is pleasant")
else:
    print("It is hot")

This task practises multiple conditions and logical checks.


2. A simple quiz


Make a mini quiz with one question. If the user enters the correct answer, congratulate them, otherwise print "wrong":



answer = input("What is 5 + 7? ")

if answer == "12":
    print("Correct! ✅")
else:
    print("Wrong, try again.")

This task teaches how to combine conditions with data entered by the user.


3. An extra challenge


  • Extend the quiz with 2 more questions.
  • Use elif to check several possible answers.
  • Show different messages for a correct and a wrong answer.

Practising these mini tasks prepares you for more complex projects in Python.


4. Challenging tasks with random numbers


In the following tasks you will practise combining if/elif/else branching with functions from the random module for generating random numbers.


4.1. Guessing a number

Make a game where the computer randomly chooses a number between 1 and 20, and the user tries to guess it. The program should print:

  • "Too big" if the user's input is bigger than the random number
  • "Too small" if the input is smaller
  • "Correct!" if they guess the number

import random

secret_number = random.randint(1, 20)
guess = int(input("Guess a number between 1 and 20: "))

if guess > secret_number:
    print("Too big")
elif guess < secret_number:
    print("Too small")
else:
    print("Correct! Congratulations!")

4.2. Simulating a die roll

The program should simulate rolling a standard die (1-6) and print a message depending on the number:

  • 1 or 2 → "A bad result"
  • 3 or 4 → "An average result"
  • 5 or 6 → "An excellent result"

import random

die = random.randint(1, 6)

if die <= 2:
    print("A bad result:", die)
elif die <= 4:
    print("An average result:", die)
else:
    print("An excellent result:", die)

4.3. A mini quiz with a random question

Extend the previous quiz so that the program randomly chooses one of 3 questions, and the user enters an answer. The program checks whether the answer is correct.


import random

questions = ["What is 3 + 4?", "What is 5 * 2?", "What is 10 - 6?"]
answers = ["7", "10", "4"]

i = random.randint(0, 2)
user_answer = input(questions[i] + " ")

if user_answer == answers[i]:
    print("Correct! ✅")
else:
    print("Wrong. ❌")

These tasks help you apply what you have learned in practical situations, combining branching, logical checks and randomness. You can try to extend the game further with more attempts or rewards for a correct answer.

A gallery of illustrations for Python tasks — branching and random numbers

An overview of the pictures for the tasks with branching and random numbers that students use in exercises and mini projects.

Harder tasks for independent work — preparing for the qualifications

These are more demanding tasks from the first phase of the qualifications for informatics competitions. They cover the material up to branching (no loops), and the solutions are deliberately not given — the tasks are meant for independent work.


Task 8 — Towers of blocks

Description: Two friends already have towers of heights A and B. They want to make a new pair of towers according to one of two ideas: (A1, B1) or (A2, B2). Making a tower 1 unit taller or shorter takes time Tx for the first friend and Ty for the second.

They start at the same time, and each works on one tower. The finishing time is the moment when both are done. They choose who works on which tower, and also which of the existing towers becomes which tower of the new pair. Calculate the smallest possible time they need to turn the existing towers into one of the pairs (A1, B1) or (A2, B2).

Input:
1) A B
2) A1 B1
3) A2 B2
4) Tx Ty

Output: One number — the smallest time.

Hint: For each idea (A1, B1) and (A2, B2) try all the arrangements:

  • tower A becomes A1 and tower B becomes B1 – or the other way round (A becomes B1, B becomes A1);
  • the first friend (Tx) works on tower A and the second (Ty) on tower B – or the other way round.

For each arrangement the time is the bigger of the two times, for example max(|A − A1|·Tx, |B − B1|·Ty). The answer is the smallest of all these times (8 possibilities). In test example 1: A becomes 28 and B becomes 18 → max(18·1, 2·10) = 20.

Test example 1 (from the task):

Input:
10 20
18 28
21 31
1 10

Output:
20

Test example 2 (extra):

Input:
7 4
9 13
14 12
3 2

Output:
15

Task 17 — Plums

Description: Janko takes 2 plums at a time and Nataša 3, as long as there are at least 5. When fewer than 5 are left, Marko takes the rest.

Input: N — the starting number of plums.

Output: The number of plums taken by Janko, Nataša and Marko, and how many people took at least one plum.

Hint: No loops — the task can be solved by analysing cases (N = 0–4, 5–7, 8–10, …) and branching: check how many "sets" (2+3) can be taken, then deal with the rest.

Test example 1:

Input:
4

Output:
0 0 4 1

Test example 2:

Input:
10

Output:
4 6 0 2

Task 19 — A football group (table)

Description: Four teams (A, B, C, D) play 6 matches against each other. The result of each match is read. Calculate the points and the goal difference of each team.

Input: 6 match results: AB, CD, AC, BD, AD, BC. Each result is a pair of numbers: the goals of the first team, then of the second.

Output: For A, B, C and D in order — the number of points and the goal difference.

Hint: For each match: a win = 3 points, a draw = 1 point, a loss = 0. Also add up the goals scored and conceded to get the goal difference (scored − conceded) for each team.

Test example 1:

Input:
2 1
0 0
1 1
3 0
0 2
2 2

Output (format: points goal difference for A B C D):
4 -1 4 2 3 0 4 -1

Task 4 — Do three lengths form a triangle?

Input: Three numbers a, b, c.

Task: Check whether a + b > c, a + c > b and b + c > a. If all are true → print "YES", otherwise "NO".

Hint: Use a combination of logical operators and several conditions (all three inequalities must be true).

Test example 1:

Input:
3 4 5

Output:
YES

Test example 2:

Input:
2 2 5

Output:
NO

Task 5 — The dominant RGB component

Input: Three numbers R, G, B (0–255).

Task: Find which component is the biggest. If several are the biggest — print both or all three.

Hint: Deal with all three cases: (1) all different, (2) two equal biggest ones, (3) all three equal.

Test example 1:

Input:
120 200 80

Output:
Green

Test example 2:

Input:
255 255 100

Output:
Red and Green

For more tasks visit: Qualifications for district competitions — tasks