• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 2. Conditions

Difficulty level:

Task«Fortress protection»

imagine that you are creating a simple calendar application. One of the most important functions of such an application & mdash; Do not allow the user to enter non -existent dates. For example, on February 30 or April 31.

rn

Write a program that asks the user to enter three numbers: day, month and year. Your task & mdash; Check if the introduced date is real. The program should take into account that in different months there are different days of days, as well as that there are leap years in which in February 29 days.

rn

rules for the rules for Inspections:

rn
    rn
  1. months with 31 days: January (1), March (3), May (5), July (7), August (8), October (10), December (12).
  2. rn
  3. months with 30 days: April (4), June (6), September, November (11).
  4. rn
  5. February (2) has 28 days in the usual year and 29 days in the temporal. Style = "Font-Size: 14pt;"> the year is a leap if it is divided by 4, but is not divided into 100, or if it is divided into 400.
  6. rn rn The program should display the "correct date" if the date exists, and the "incorrect date" otherwise.

    Input format

    Day (integer)
    month (integer)
    year (whole Number)

    Output format

    The result of the check (line)

    Example

    Input

    29
    2
    2024

    Output

    The correct date

    Hint

    There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Запрашиваем у пользователя три числа: день, месяц, год
day = int(input())
month = int(input())
year = int(input())

# Проверяем базовую корректность введенных данных
if month < 1 or month > 12:
    print("Некорректная дата")
else:
    # Проверяем корректность дня для каждого месяца
    
    # Месяцы с 31 днем: январь(1), март(3), май(5), июль(7), август(8), октябрь(10), декабрь(12)
    if month in [1, 3, 5, 7, 8, 10, 12]:
        if day < 1 or day > 31:
            print("Некорректная дата")
        else:
            print("Корректная дата")
    
    # Месяцы с 30 днями: апрель(4), июнь(6), сентябрь(9), ноябрь(11)
    elif month in [4, 6, 9, 11]:
        if day < 1 or day > 30:
            print("Некорректная дата")
        else:
            print("Корректная дата")
    
    # Февраль (2) - особый случай
    elif month == 2:
        # Проверяем, является ли год високосным
        is_leap_year = False
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
            is_leap_year = True
        
        # Проверяем корректность дня для февраля
        if is_leap_year:
            # Високосный год - до 29 дней
            if day < 1 or day > 29:
                print("Некорректная дата")
            else:
                print("Корректная дата")
        else:
            # Обычный год - до 28 дней
            if day < 1 or day > 28:
                print("Некорректная дата")
            else:
                print("Корректная дата")

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!