U
U
UI2018-08-31 21:28:35
Python
UI, 2018-08-31 21:28:35

Task in Python?

Hello everyone, I'm taking a course on Stepik.org (no ads, don't ban me) about Python. I can't solve the problem.
The task itself:

At the Institute of Bioinformatics, a robot moves around the office. Recently, students from a group of programmers wrote a program for him, according to which the robot, when it enters a room, counts the number of programmers in it and says it out loud: "n programmers."
To make it sound right, for each n you need to use the correct word ending.
Write a program that reads an integer n (non-negative) from user input, outputs this number to the console along with the correct word "programmer", so that the robot can communicate normally with people, for example: 1 programmer, 2 programmers, 5 programmers .
There can be a lot of programmers in a room. Check that your program will correctly handle all cases up to at least 1000 people.

Here is my code:
a=int(input())
if a%5==0:
    print(a ,'программистов')
if a>0 and a%2==0:
    print(a, 'программиста')
if not a%5==0 and not a%2==0:
    print(a, 'программист')

When running the code on the site itself, it gives this:
5b8988a00422c703214656.pngOutput / Input:
5b89896dc85ea447070406.png
What is my mistake? Testing code in IDLE Python3.7 . Help me please!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Teslaman, 2018-08-31
@Teslaman

The task is boring, so I wrote a program to generate programs for this task.
To get a program that solves your problem, copy the following code into a ".py" file and run it. The script will create a new task.py script, which will contain the solution to your task.

import sys


def main(programmers: int):
    with open("task.py", "w") as f:
        f.write("a = int(input('Введите количество программистов: '))\n\n")
        f.write(f"if a == 0:\n\tprint(0, 'программистов')\n")
        for i in range(1, programmers + 1):
            programmers = "программист" + get_ending(i)
            f.write(f"elif a == {i}:\n\tprint({i}, '{programmers}')\n")
        print("Твоя программа готова! Запусти её командой: python task.py")


def get_ending(num: int) -> str:
    if num % 100 in {11, 12, 13, 14}:
        return "ов"
    elif num % 10 in {0, 5, 6, 7, 8, 9}:
        return "ов"
    elif num % 10 in {2, 3, 4}:
        return "а"
    elif num % 10 in {1}:
        return ""
    else:
        raise AssertionError("Unexpected error")


if __name__ == '__main__':
    try:
        programmers = abs(int(input(
            "Для какого количества программистов сгенерировать решения?\nВведи целое число: ")))
    except ValueError:
        print("Количество программистов должно быть целым числом!")
        sys.exit(1)
    main(programmers)

D
Dimonchik, 2018-08-31
@dimonchik2013

have fun with the number 126
since you're here - type the following phrase on a piece of paper and hang the
phrase:
the program should give the correct results,
and DO NOT PRODUCE the wrong ones

, all beginner progers have problems with the second part

B
bbkmzzzz, 2018-09-03
@bbkmzzzz

3 options for nouns after numerals.
The rules are simple:
ends with 1, but not 11
ends with 2 3 4, AND NOT in the range 10-20 (inclusive)
all other
forms of words correspond to the numbers 1,2,5
for example:
1 crow
2 crows
5 crows
take the remainder of the division the desired number by 10 and 100 and run through the conditions.
so you can check any numbers, not up to 1000, including negative ones

words = ['ворона', 'вороны', 'ворон']

def plural(num, words):
    if num == 0:
        return words[2]
    d10 = num % 10
    d100 = num % 100
    if d10 == 1 and d100 != 11:
        return words[0]
    elif (2 <= d10 <= 4) and (d100 < 10 or d100 > 20):
        return words[1]
    else:
        return words[2]
num = 21
print(num, plural(num, words))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question