In if, when the user puts another state that is not specified, instead of entering Else, he enters if and continues the code normally

Asked

Viewed 50 times

-5

The code is like this:

est = input('Qual estado reside atualmente? ')  
if est == 'São Paulo' or 'SP' or 'sao paulo' or 'São paulo':  
    email = input('Digite o seu e-mail: ')   
    tel = input('Digite o seu celular pessoal: ')  
    tel2 = input('Digite seu telefone comercial ou residencial: ')  
else:  
    print('Não atendemos na sua localidade!')  
    exit()  

2 answers

-1

You could write the if conditions the same way you wrote the first conditional. Follow the example:

if est == 'São Paulo' or  est == 'SP' or  est == 'sao paulo' or  est == 'São paulo': 

I am highlighting just the excerpt of if, how it could be rewritten in a way that can be evaluated correctly.

In his code as the conditional is written in this form:

if est == 'São Paulo' or 'SP':

So the comparison of if evaluates the literal value 'SP'.

Like any string other than the empty value "" has the return value true, your parole is always assessed as true.

-3


Hello, the problem is happening in the condition of if. It is necessary that you add est == before all possibilities:

if est == 'São Paulo' or est == 'SP' or est == 'sao paulo' or est == 'São paulo': 

Changing this way will enter the if correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.