I
I
Ivan2014-11-11 17:13:36
Python
Ivan, 2014-11-11 17:13:36

Why does an already suppressed condition end up in else?

a=int(input())
if a==1:
    print(a, 'программист')
b=a%10
c=a%100
if (b==1 and (not c==11) and (not a==1)):
    print(a, 'программист')
if((b==2 or b==3 or b==4) and (not(c==12 or c==13 or c==14))):
    print(a, 'программиста')
else:
    print(a, 'программистов')

does not pass the test for one, it falls into else

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2014-11-11
@tsarevfs

The first if has no else. All the following checks are performed with a=1 in the usual way. In such cases, it is convenient to use the abbreviation else if - elif

a = int(input())
b = a % 10
c = a % 100
if a == 1:
    print(a, 'программист')
elif (b == 1 and not c == 11 and not a == 1):
    print(a, 'программист')
elif((b == 2 or b == 3 or b == 4) and not(c == 12 or c == 13 or c == 14)):
    print(a, 'программиста')
else:
    print(a, 'программистов')

A
Alexander, 2014-11-13
@Survtur

Because your else only applies to the last if.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question