msmsms Posted August 13, 2012 at 01:00 PM Report Share #472137 Posted August 13, 2012 at 01:00 PM (edited) fiz o seguinte programa para devolver os numeros de 0 até 5 mas não estou a conseguir fazer correr podem-me dizer o que está errado ou o que está em falta para isto correr? def numeros(): i=0 while i<=5: print 'numero:'+i i = i + 1 else: print 'terminou' if __name__=='__main__': numeros() Edited August 13, 2012 at 07:38 PM by thoga31 GeSHi Link to comment Share on other sites More sharing options...
KTachyon Posted August 13, 2012 at 01:10 PM Report Share #472139 Posted August 13, 2012 at 01:10 PM print 'numero:',i “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 Link to comment Share on other sites More sharing options...
msmsms Posted August 13, 2012 at 01:18 PM Author Report Share #472140 Posted August 13, 2012 at 01:18 PM (edited) print 'numero:',i de facto com essa alteração o programa corre e os numeros são apresentados mas no exemplo que tenho num livro tem o +i e não ,i porque será? Edited August 13, 2012 at 01:19 PM by msmsms Link to comment Share on other sites More sharing options...
KTachyon Posted August 13, 2012 at 01:27 PM Report Share #472142 Posted August 13, 2012 at 01:27 PM Utilizando o + se fizeres: print 'numero:' + str(i) De certeza que, no livro, estás a concatenar um inteiro com uma string? “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 Link to comment Share on other sites More sharing options...
msmsms Posted August 13, 2012 at 02:13 PM Author Report Share #472152 Posted August 13, 2012 at 02:13 PM (edited) Utilizando o + se fizeres: print 'numero:' + str(i) De certeza que, no livro, estás a concatenar um inteiro com uma string? no livro é a coisa está assim: i=0 while i<=5: print "numero:"+'i' i=i+1 else: print "acabou!" as plicas do (i) são meio tortas parecem acentos `i` no livro é a coisa está assim: i=0 while i<=5: print "numero:"+`i` i=i+1 else: print "acabou!" as plicas do (i) são meio tortas parecem acentos `i` já vi que era eu que não estava a fazer as plicas `i` em código como deve ser... acho que é mais facil fazer a virgula antes em vez do mais a virgula funciona com ou sem plicas no i já o mais só funciona se forem colocadas as plicas tornas no i print "numero:"+`i` - corre tudo bem print "numero:"+i - dá erro print "numero:",`i` - corre tudo bem print "numero:",i - corre tudo bem print 'numero:' + str(i) - corre tudo bem print 'numero:' + str(`i`) - corre tudo bem Edited August 16, 2012 at 11:03 AM by brunoais geshi Link to comment Share on other sites More sharing options...
KTachyon Posted August 13, 2012 at 02:52 PM Report Share #472163 Posted August 13, 2012 at 02:52 PM Pois, exactos, podes fazer com os acentos. Atenção que a vírgula coloca um espaço entre as variáveis. “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 Link to comment Share on other sites More sharing options...
thoga31 Posted August 13, 2012 at 07:44 PM Report Share #472191 Posted August 13, 2012 at 07:44 PM De facto existem várias formas de fazer o output de um texto com variáveis pelo meio. O meu método favorito, por exemplo, é o do format: print "numero: {0}".format(i) O mais comum no Python 2 é, salvo erro: print "numero: %d" %i Todos os métodos que te deram certo são correctos, mas eu aconselho-te a escolheres um método concreto, aquele que mais gostes. 😉 Knowledge is free! Link to comment Share on other sites More sharing options...
Yana Posted August 14, 2012 at 04:12 PM Report Share #472267 Posted August 14, 2012 at 04:12 PM De facto existem várias formas de fazer o output de um texto com variáveis pelo meio. O meu método favorito, por exemplo, é o do format: print "numero: {0}".format(i) O mais comum no Python 2 é, salvo erro: print "numero: %d" %i Todos os métodos que te deram certo são correctos, mas eu aconselho-te a escolheres um método concreto, aquele que mais gostes. 😉 Exato, normalmente faço: print "numero: %d" %i Link to comment Share on other sites More sharing options...
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