• 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
  • к

Занятие 1. Entering and output of data

Difficulty level:

Task«Number in words»

Write a program that accepts the number from 0 to 9999 and displays it with words in Russian. If you introduce a number more or less than the range, but also if you introduce not a number, display the “Entering is not true” screen

Input format

One line containing a single number from 0 to 9999 inclusive.

Output format

One line containing a verbal representation of the number in quotation marks.

Example

Input

1234

Output

One thousand two hundred and thirty -four

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
Test 11
Test 12
Test 13
Developer’s solution
def number_to_words(n):
    """
    Преобразует число от 0 до 9999 в его словесное представление на русском языке.
    """
    if not 0 <= n <= 9999:
        return "ввод не верный"

    if n == 0:
        return "ноль"

    units = {
        1: "один", 2: "два", 3: "три", 4: "четыре", 5: "пять",
        6: "шесть", 7: "семь", 8: "восемь", 9: "девять"
    }
    teens = {
        10: "десять", 11: "одиннадцать", 12: "двенадцать", 13: "тринадцать",
        14: "четырнадцать", 15: "пятнадцать", 16: "шестнадцать",
        17: "семнадцать", 18: "восемнадцать", 19: "девятнадцать"
    }
    tens = {
        20: "двадцать", 30: "тридцать", 40: "сорок", 50: "пятьдесят",
        60: "шестьдесят", 70: "семьдесят", 80: "восемьдесят", 90: "девяносто"
    }
    hundreds = {
        100: "сто", 200: "двести", 300: "триста", 400: "четыреста",
        500: "пятьсот", 600: "шестьсот", 700: "семьсот",
        800: "восемьсот", 900: "девятьсот"
    }

    parts = []

    # Обработка тысяч
    if n >= 1000:
        thousand_val = n // 1000
        if thousand_val == 1:
            parts.append("одна тысяча")
        elif thousand_val == 2:
            parts.append("две тысячи")
        elif thousand_val in [3, 4]:
            parts.append(units[thousand_val] + " тысячи")
        else: # 5-9
            parts.append(units[thousand_val] + " тысяч")
        n %= 1000

    # Обработка сотен
    if n >= 100:
        parts.append(hundreds[n // 100 * 100])
        n %= 100

    # Обработка десятков и единиц
    if n >= 20:
        parts.append(tens[n // 10 * 10])
        n %= 10
        if n > 0:
            parts.append(units[n])
    elif n >= 10:
        parts.append(teens[n])
    elif n > 0:
        parts.append(units[n])
        
    return " ".join(parts)

# Основная часть программы для считывания ввода и вывода результата
try:
    num = int(input())
    result = number_to_words(num)
    print(result)
except ValueError:
    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, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!