Jump to content

Recommended Posts

Posted (edited)

estou com problemas para receber da parte do utilizador uma string como resposta

o programa dá erro

alguém me consegue explicar porquê que isso acontece?

porquê que mal eu coloco 's' na consola o programa dispara logo um erro e não devolve True?

# para o utilizaor responder sim ou nao
def sim_ou_nao():

questao = input('tem nacionalidade portuguesa? (s/n):')

if questao == 's':
    print True
else:
    print False

if __name__=='__main__':
sim_ou_nao()

Edited by thoga31
GeSHi
Posted (edited)

http://docs.python.org/library/functions.html#input

Equivalent to eval(raw_input(prompt)).

This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Consider using the raw_input() function for general input from users.

Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted

Quem utiliza Python 2 deve utilizar o raw_input, como já foi referido.

Aproveito para deixar uma dica de optimização. Em vez destas quatro linhas...

if questao == 's':
    print True
else:
    print False

... pondera fazer isto:

print questao == 's'

Knowledge is free!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.