Rozwiązania zadań – Python

Exercise Solutions – Python

Zadanie 1 — największa z trzech liczb

Rozwiązanie

a = int(input("Podaj pierwszą liczbę: "))
b = int(input("Podaj drugą liczbę: "))
c = int(input("Podaj trzecią liczbę: "))

if a >= b and a >= c:
    print("Największa liczba to:", a)
elif b >= a and b >= c:
    print("Największa liczba to:", b)
else:
    print("Największa liczba to:", c)

Wyjaśnienie

Program porównuje liczby parami:

Użycie operatora >= pozwala uwzględnić sytuację, gdy liczby są równe.


Zadanie 2 — liczba samogłosek w tekście

Rozwiązanie

tekst = input("Podaj tekst: ")

samogloski = "aeiouyAEIOUY"
licznik = 0

for znak in tekst:
    if znak in samogloski:
        licznik += 1

print("Liczba samogłosek:", licznik)

Wyjaśnienie

To proste i eleganckie rozwiązanie, które działa dla dowolnego tekstu.


Zadanie 3 — funkcja obliczająca pole kwadratu

Rozwiązanie

def pole_kwadratu(a):
    return a * a

bok = int(input("Podaj długość boku kwadratu: "))
print("Pole kwadratu:", pole_kwadratu(bok))

Wyjaśnienie

Wzór na pole kwadratu to a × a. Funkcja zwraca wynik obliczenia, a następnie program wypisuje obliczone pole.


Zadanie 4 — lista zakupów

Rozwiązanie

lista = []

for i in range(5):
    produkt = input("Podaj produkt: ")
    lista.append(produkt)

print("\nLista zakupów:")
for p in lista:
    print("-", p)

Wyjaśnienie


Zadanie 5 — gra „Zgadnij liczbę”

Rozwiązanie

import random

tajna = random.randint(1, 20)

while True:
    strzal = int(input("Zgadnij liczbę (1–20): "))

    if strzal < tajna:
        print("Za mało!")
    elif strzal > tajna:
        print("Za dużo!")
    else:
        print("Brawo! Trafiłeś!")
        break

Wyjaśnienie

Exercise 1 — the largest of three numbers

Solution

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))

if a >= b and a >= c:
    print("The largest number is:", a)
elif b >= a and b >= c:
    print("The largest number is:", b)
else:
    print("The largest number is:", c)

Explanation

The program compares the numbers in pairs:

Using the >= operator handles the case when numbers are equal.


Exercise 2 — number of vowels in a text

Solution

text = input("Enter a text: ")

vowels = "aeiouyAEIOUY"
counter = 0

for letter in text:
    if letter in vowels:
        counter += 1

print("Number of vowels:", counter)

Explanation

This is a simple and elegant solution that works for any text.


Exercise 3 — a function that calculates the area of a square

Solution

def square_area(a):
    return a * a

side = int(input("Enter the length of the square's side: "))
print("Area of the square:", square_area(side))

Explanation

The formula for the area of a square is a × a. The function returns the result of the calculation, and the program then prints the calculated area.


Exercise 4 — shopping list

Solution

shopping_list = []

for i in range(5):
    item = input("Enter a product: ")
    shopping_list.append(item)

print("\nShopping list:")
for p in shopping_list:
    print("-", p)

Explanation


Exercise 5 — the "Guess the number" game

Solution

import random

secret = random.randint(1, 20)

while True:
    guess = int(input("Guess the number (1–20): "))

    if guess < secret:
        print("Too low!")
    elif guess > secret:
        print("Too high!")
    else:
        print("Well done! You guessed it!")
        break

Explanation