M
M
Montik2020-08-17 23:01:24
Python
Montik, 2020-08-17 23:01:24

Why nested if statements don't fire?

Hello. For luminaries, the question is elementary, but still:
The condition is a conceived number, only an if statement and two nested operators, two options - guessed (correctly) and not guessed (incorrectly)
For some reason nested operators do not work, please explain, please be so kind what is missing?

number = 43
guess = int(input('Введите число :'))

if guess == number:
  
  if True:
        print('Число выбрано верно')
  if False:
        print('Число выбрано неверно')
        
print('Завершено')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Proskurin, 2020-08-17
@Vlad_IT

You misunderstand conditional statements a bit

if guess == number:
    print('Число выбрано верно')
else:
    print('Число выбрано неверно')

In your case, the block
if True:
    print('Число выбрано верно')

is always executed, because True is true. And this block
if False:
    print('Число выбрано неверно')

never executed. And since if True you have inside if guess == number, the inscription is displayed only if the number is chosen correctly, otherwise nothing is displayed

S
soremix, 2020-08-17
@SoreMix

You don't need more if for the current task.

if guess == number:
    print('Число выбрано верно')
else:
    print('Число выбрано не верно')

Regarding the minimum entry from the book, the if construct can be decomposed into an if-elif-else maximum, for example, for this:
if guess == number:
    print('Число выбрано верно')
elif guess == 0:
    print('Нельзя ввести ноль')
else:
    print('Число выбрано не верно')

But often there are situations when you do not need any additional actions in the algorithm, as well as additional conditions in the form of elif and / or else, but just one check will be enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question