H
H
Hanami Mirai2022-04-21 16:52:55
Python
Hanami Mirai, 2022-04-21 16:52:55

Logical operators in Python?

Hello everyone, I decided to write a simple program using a logical operator.
When writing code like this, it gives a syntax error:

age = float(input('Введите число:'))

if age <= 10:
    print('Числ меньше или равно 10')
elif age > 10 or <= 25:
    print('Число больше 10 или меньше или равно 25-и')
elif age > 25:
    print('Число больше 25')


And with this, the third instruction is not executed if the number is greater than 25
age = float(input('Введите число:'))

if age <= 10:
    print('Числ меньше или равно 10')
elif age > 10  or age <= 25:
    print('Число больше 10 или меньше или равно 25-и')
elif age > 25:
    print('Число больше 25')

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Markin, 2022-04-21
@HanamiMirai

In the first case, they don’t write like this: They write like this: And even better like this: In the second case, 25 already fell under the condition : Therefore, the following if is not executed: As a result, you can do this:
elif age > 10 or <= 25
elif age > 10 or age <= 25
elif 10 < age <= 25
elif age > 10 or age <= 25
elif age > 25

if age <= 10:
    print('Числ меньше или равно 10')
elif 10 < age <= 25:
    print('Число больше 10 или меньше или равно 25-и')
else:
    print('Число больше 25')

A
Alexander Nesterov, 2022-04-21
@AlexNest

At least for "greater than X, less than/[equal to} Y" you can use a trailing comparison.

age = float(input('Введите число:'))

if age <= 10:
    print('Число меньше или равно 10')
elif  10 < age <= 25:
    print('Число больше 10 или меньше или равно 25-и')
elif age > 25:
    print('Число больше 25')

S
seven5674, 2022-04-21
@seven5674

elif age > 10 and age <= 25:
or
elif 10 < age <= 25:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question