Except is not capturing the exception

Asked

Viewed 100 times

-4

mensagem_de_modalidade = int (input ('''
Como você quer jogar ?
1)Solo
2)Em dupla
3)Em trio'''))


try :
        if mensagem_de_modalidade == 1 :
                print ("oi")
        if mensagem_de_modalidade == 2 :
                print ("hi")
        if mensagem_de_modalidade == 3 :
                print ("sla")


except :
        print ("TESTE")
  • 3

    Maniero answered this in https://answall.com/a/440344/3635, it makes no sense to use Try for something that is not an exception, you have to ask again if you don’t understand something, I commented there where you already asked the first question.

  • It is that when it enters in the exception of "except" the program shows what it has inside the block and closes. Now if I had used "Else" the program would continue running normally, which I did not want

  • @Smaug can you please remove the acceptance of my answer ? When you read the answer and accepted it, it was not correct.

1 answer

3

It turns out that what you’re trying to capture is no exception, because it’s something that’s just not inside any if. The code that could capture this not valid option should be more or less like this:

mensagem_de_modalidade = int (input ('''
Como você quer jogar ?
1)Solo
2)Em dupla
3)Em trio'''))

if mensagem_de_modalidade == 1 :
    print ("oi")
elif mensagem_de_modalidade == 2 :    
    print ("hi")
elif mensagem_de_modalidade == 3 :
    print('sla')
else:
    print("TESTE")
  • It’s just that I wanted the program to close if it came to the exception, so I thought I’d better use "except" instead of "Else", but if there’s any way that that happens using "Else", you can tell how to do it, like, so the code gets better

  • 2

    @Smaug he said, I already told you on https://answall.com/questions/440341/except-n%C3%a3o-recognize-value-as-wrong.

Browser other questions tagged

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