msmsms Posted September 27, 2012 at 08:33 AM Report #476736 Posted September 27, 2012 at 08:33 AM (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 September 28, 2012 at 07:28 PM by thoga31 GeSHi
VitorGarcia Posted September 27, 2012 at 12:00 PM Report #476767 Posted September 27, 2012 at 12:00 PM Qual é a tua versão do Python?Experimenta com raw_input em vez de input.
KTachyon Posted September 27, 2012 at 12:42 PM Report #476770 Posted September 27, 2012 at 12:42 PM (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 September 27, 2012 at 12:43 PM 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
thoga31 Posted September 28, 2012 at 07:33 PM Report #476972 Posted September 28, 2012 at 07:33 PM 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!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now