First day beginner to python, add to counter after nested loop
Neil Cerutti
neilc at norwich.edu
Tue Oct 29 16:32:50 EDT 2013
On 2013-10-29, jonas.thornvall at gmail.com
<jonas.thornvall at 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
More information about the Python-list
mailing list