Problem number 3517, about the cows in the python performers, help solve. Here is the condition: For a given number n <100, complete the phrase “On the meadow graze ...” one of the possible continuations: “n cows”, “n cow”, “n cows”, correctly inclining the word “cow”. The program should display the entered number n and one of the words: korov, korova or korovy. There must be exactly one space between the number and the word.

Closed due to the fact that off-topic participants Enikeyschik , Kromster , 0xdb , freim , aleksandr barakin 12 Feb at 14:27 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Enikeyschik, Kromster, 0xdb, freim, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

    5 answers 5

    Python 3
    1st option:

    n = int(input()) if n >= 11 and n <= 14: print(n, 'korov') else: temp = n % 10 if temp == 0 or (temp >= 5 and temp <= 9): print(n, 'korov') if temp == 1: print(n, 'korova') if temp >=2 and temp <=4: print(n, 'korovy') 

    Option 2:

     n = int(input()) if n in range(11, 15): print(n, 'korov') else: temp = n % 10 if temp in list(range(5,10))+[0]: print(n, 'korov') if temp == 1: print(n, 'korova') if temp in range(2,5): print(n, 'korovy') 
    • one
      @mctrane conditions can also be written differently: temp in range (5,10) + [0] temp in range (2,5) Just do not forget that the second parameter range in the range is no longer included. - alexlz
    • Yes, it highlights the features of Python more, changed - iproger
    • And it seems to me that the previous (traditional) version was clearer (and not too long). Those. in this problem list, range, and so on. Python features do not give anything. - avp
    • I posted both options :) I do not know how in other languages, but comparing with C ++, PHP, Java. This feature: the in operator, the addition of lists with a simple "+". - iproger
    • Hmm, pythons are different ... And at first I was surprised when I saw list (range (5,10)) + [0] python3 ... - alexlz

    A solution using only the conditional if construction and arithmetic \ logical operations.

     n = int(input()) if n % 10 == 1 and n != 11: print(n, 'korova') elif 2 <= n % 10 <= 4 and n // 10 != 1: print(n, 'korovy') else: print(n, 'korov') 

      You can use the gettext module to select the correct plural form for the current language:

       #!/usr/bin/env python3 from gettext import translation t = translation('messages', 'locale', fallback=True) for n in [0, 1, 2, 5, 10, 11, 21, 22, 111]: print(t.ngettext("there is %d cow", "there are %d cows", n) % n) 

      If run without translation, the output:

       $ python cows.py there are 0 cows there is 1 cow there are 2 cows there are 5 cows there are 10 cows there are 11 cows there are 21 cows there are 22 cows there are 111 cows 

      To get the desired output, you need to add a translation, for example, using the babel module ( pip install babel ):

       $ pybabel extract -o messages.pot . extracting messages from cows.py writing PO template file to messages.pot $ pybabel init -i messages.pot -d locale -l ru creating catalog locale/ru/LC_MESSAGES/messages.po based on messages.pot 

      After adding the translation to message.po :

       msgstr[0] "На лугу пасётся %d корова" msgstr[1] "На лугу пасётся %d коровы" msgstr[2] "На лугу пасётся %d коров" 

      To make the translation available, you need to collect messages.mo :

       $ pybabel compile -d locale compiling catalog locale/ru/LC_MESSAGES/messages.po to locale/ru/LC_MESSAGES/messages.mo 

      You can run with the Russian language:

       $ LANGUAGE=ru python cows.py На лугу пасётся 0 коров На лугу пасётся 1 корова На лугу пасётся 2 коровы На лугу пасётся 5 коров На лугу пасётся 10 коров На лугу пасётся 11 коров На лугу пасётся 21 корова На лугу пасётся 22 коровы На лугу пасётся 111 коров 

      This works because ru/LC_MESSAGES/messages.po automatically added:

       "Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && " "n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n" 

      If you change the text that needs to be translated in cows.py , you should update the directories:

       $ pybabel update -i messages.pot -d locale updating catalog locale/ru/LC_MESSAGES/messages.po based on messages.pot 

      And re-compile translations:

       $ pybabel compile -d locale compiling catalog locale/ru/LC_MESSAGES/messages.po to locale/ru/LC_MESSAGES/messages.mo 

      To add a new language, for example French:

       $ pybabel init -i messages.pot -d locale -l fr creating catalog locale/fr/LC_MESSAGES/messages.po based on messages.pot 

      Add self translation for singular and plural:

       msgstr[0] "Sur la prairie de pâture %d vache" msgstr[1] "Sur la prairie de pâture %d vaches" 

      (for example, phrases from Yandex.Translator taken)

      Update .mo files:

       $ pybabel compile -d locale compiling catalog locale/fr/LC_MESSAGES/messages.po to locale/fr/LC_MESSAGES/messages.mo compiling catalog locale/ru/LC_MESSAGES/messages.po to locale/ru/LC_MESSAGES/messages.mo 

      After that, you can run in French:

       $ LANGUAGE=fr python cows.py Sur la prairie de pâture 0 vache Sur la prairie de pâture 1 vache Sur la prairie de pâture 2 vaches Sur la prairie de pâture 5 vaches Sur la prairie de pâture 10 vaches Sur la prairie de pâture 11 vaches Sur la prairie de pâture 21 vaches Sur la prairie de pâture 22 vaches Sur la prairie de pâture 111 vaches 
      • There are two cows in the meadow. - user239133
      • @AlexanderZonov: what to write in message.po is already a question for the site Russian language :) Examples confirm the “graze” option - jfs
       n = int(input()) base = 'korov' suffix = '' if (n % 10 == 1) and n != 11: suffix = u'a' elif n % 10 in ([2, 3, 4]) and n not in range(10, 21): suffix = u'y' print(str(n), "{}{}".format(base, suffix)) 

        For fans of "one-liners", the option without if :

         print 'korov' + {n % 10 == 1 and n != 11: 'a', n % 10 in [2, 3, 4] and n not in range(10, 15): 'y'}.setdefault(1, '') #44 #korovy