The `Else` operator does not process an incorrect user input

Asked

Viewed 61 times

-3

I’m doing a Python registration system.
The problem is that when the user misspells his sex in a input, the operator else makes a mistake.

from time import sleep  
while True:  
    print('\033[1;36m')  
    opcoes = '[1]- SE CADASTRAR\n[2]- TABUADA\n[3]- SAIR'  
    print(opcoes)  
    print('\033[m')  
    print('\033[1;37m')  
    op = int(input(''))  
    if op == 1:  
        nome = input('Crie um usuário: ')  
        senha = int(input('Crie uma senha com apenas números: '))  
        MF = str(input('Digite seu sexo: [M/F] ')).strip().upper()[0]  
    if MF == 'm' or 'M' or 'f' or 'F':  
        print('Sexo válido!')  
    else:  
        print('Sexo inválido!')  
    if op == 2:  
        numero = int(input('Digite um número: '))  
        for tab in range(1,11):  
            print('{} ×{:2} = {}'.format(numero,tab,numero*tab))  
    if op == 3:  
        print('\033[1;32m')  
        print('Saindo do script.')  
        sleep(2.5)  
        print('Loading...')  
        sleep(5.5)  
        print('Precione enter para sair.')  
        break  
        exit  
  • 1

    Change the conditional statement if MF == 'm' or 'M' or 'f' or 'F': for if MF in 'MF': .No need to test with lowercase because here MF = str(input('Digite seu sexo: [M/F] ')).strip().upper()[0] MF will return converted characters to uppercase.

1 answer

0


There is an error in Else due to a syntax error in your if:

if MF == 'm' or 'M' or 'f' or 'F': 
    print('Sexo válido!') 
else: 
    print('Sexo inválido!')

the correct would be:

if MF == 'M' or MF == 'F':
    print('Sexo válido!')
else:
    print('Sexo inválido!')

Browser other questions tagged

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