Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: First day beginner to python, add to counter after nested loop Date: 29 Oct 2013 20:32:50 GMT Organization: Norwich University Lines: 63 Message-ID: References: <4d1c9a55-310b-41b7-8271-435fd095ce70@googlegroups.com> <7e0b17ea-3a79-45e7-aefc-795f3f34af95@googlegroups.com> <20e6a79f-2d0e-4e78-8af6-607375eca676@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net DXWsfqmP86CEUn6ypUJeTQ3aDqPt0OaUw7lh7zmAJskxrExAaV Cancel-Lock: sha1:xhGwNcbrWBZyMRwIBnvb34yCt+4= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: csiph.com comp.lang.python:57972 On 2013-10-29, jonas.thornvall@gmail.com wrote: > Got the script working though :D, good start. It seem though > that Python automaticly add linebreaks after printout. Is there > a way to not have print command change line? Or must i build up > a string/strings for later printout? print takes an keyword argument, called end, that defaults to "\n". You can provide something else: print("xzzz", end="") > #!/usr/bin/python > import math > # Function definition is here > def sq(number): > square=1; Get in the habit of not using the semicolon to end lines. Python doesn't need them, except when two statements appear without a newline between them. > factor=2; > exponent=2; > print(x,"= "); That ought to be print(number, "= ", end="") There's no need to refer to global x when you've passed it in as number. > while (number>3): > while (square<=number): > factor+=1; > square=math.pow(factor,exponent); You don't want to use math.pow. Just use pow or the ** operator. square = factor**exponent > factor-=1; > print(factor,"^2"); > square=math.pow(factor,exponent); > number=number-(factor*factor); Analogous with factor += 1, you can do number -= factor * factor Note the usual spacing of python operators and identifiers. > square=1; > factor=1; > print("+",number); > return A bare return at the end of a Python function is not needed. All functions return None if they fall off the end. -- Neil Cerutti