First day beginner to python, add to counter after nested loop

J

jonas.thornvall

I have a 25 row javascript that i try to convert to python to get into the language but i run into problem i do not understand howto reach outer loop after finnish inner loop, in fact i do not understand when finished. The javascript i try to conver is below.

#!/usr/bin/python
import math
# Function definition is here
def sq(number):

square=1;
factor=2;
exponent=2;
print(x,"= ");
while (number>3):
while (square<=number):
factor+=1;
square=math.pow(factor,exponent);
----now i want to add conter when loop finished, no end like basic????
??? factor--;
??? print(factor," ");
??? square=math.pow(factor,exponent);
??? number=number-(factor*factor);
??? square=1;
??? factor=1;
---here does second loop finish
return

print("Exp=x^2");
for x in range (22,23):
sq(x);



Here is the javascript with loop i try to convert.

<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>
<SCRIPT language=Javascript>
function sq(number){
square=1;
factor=2;
exponent=2;
document.write(x,"= ");
while (number>3){

while (square<=number)
{ factor++;
square=Math.pow(factor,exponent);

}
factor--;
document.write(factor," ");
square=Math.pow(factor,exponent);
number=number-(factor*factor);
square=1;
factor=1;
}
document.write("+",number,"<BR>");
}
document.write("Exp=x^2<BR>");
for (x=1;x<100;x++){
sq(x);
}
</script>
<BODY>
</BODY>
</HTML>
 
N

Neil Cerutti

I have a 25 row javascript that i try to convert to python to get into the language but i run into problem i do not understand howto reach outer loop after finnish inner loop, in fact i do not understand when finished. The javascript i try to conver is below.

#!/usr/bin/python
import math
# Function definition is here
def sq(number):

square=1;
factor=2;
exponent=2;
print(x,"= ");
while (number>3):
while (square<=number):
factor+=1;
square=math.pow(factor,exponent);
----now i want to add conter when loop finished, no end like basic????
??? factor--;
??? print(factor," ");
??? square=math.pow(factor,exponent);
??? number=number-(factor*factor);
??? square=1;
??? factor=1;
---here does second loop finish
return

print("Exp=x^2");
for x in range (22,23):
sq(x);



Here is the javascript with loop i try to convert.

Output:
7 = 2 + 3
number square factor exponent
7 1 2 2
9 3
3 1 1
<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>
<SCRIPT language=Javascript>
function sq(number){
square=1;
factor=2;
exponent=2;
document.write(x,"= ");
while (number>3) {
while (square<=number) {
factor++;
square=Math.pow(factor,exponent);
}
factor--;
document.write(factor," ");
square=Math.pow(factor,exponent);
number=number-(factor*factor);
square=1;
factor=1;
}
document.write("+",number,"<BR>");
}
document.write("Exp=x^2<BR>");
for (x=1;x<100;x++){
sq(x);
}
</script>
<BODY>
</BODY>
</HTML>

What on does function sq do? Squank? Squarp? Squijel?

For number 7, I'm thinking I get output:

7 = 2 + 3
 
J

jonas.thornvall

Den tisdagen den 29:e oktober 2013 kl. 19:09:03 UTC+1 skrev Neil Cerutti:
Output:

7 = 2 + 3

number square factor exponent

7 1 2 2

9 3

3 1 1










What on does function sq do? Squank? Squarp? Squijel?



For number 7, I'm thinking I get output:



7 = 2 + 3

It write out numbers on modular? square form, just copy past the HTML embedded javascript into text, save as HTML run i brower and you see.

Decimal base
1234=1000+200+30+4 i bad at terminiology but let us call modular base 10 form.

The script convert to modular square form number and write out.
1234= 35 3 +0
35^2 + 3^3 = 1225 + 9 =1234

It is very basic
 
J

jonas.thornvall

Den tisdagen den 29:e oktober 2013 kl. 19:23:28 UTC+1 skrev (e-mail address removed):
Den tisdagen den 29:e oktober 2013 kl. 19:09:03 UTC+1 skrev Neil Cerutti:




It write out numbers on modular? square form, just copy past the HTML embedded javascript into text, save as HTML run i brower and you see.



Decimal base

1234=1000+200+30+4 i bad at terminiology but let us call modular base 10 form.



The script convert to modular square form number and write out.

1234= 35 3 +0

35^2 + 3^3 = 1225 + 9 =1234



It is very basic

And it work for any exponential form square, cubic etc. I want to implementin python since javascript lack bignumb, and diskread write function.

Exp=x^2
7777777777= 88191 353 26 3 +2
Exp=x^3
7777777777= 1981 153 33 10 4 3 3 +4
Exp=x^4
7777777777= 296 100 33 12 9 5 4 3 3 2 2 2 +12
Exp=x^5
7777777777= 95 33 15 9 6 5 5 3 3 2 2 +9
Exp=x^6
7777777777= 44 28 18 13 9 7 6 5 5 4 3 3 3 3 2 2 2 2 2 2 2 2 +64
Exp=x^7
7777777777= 25 20 16 14 11 7 5 4 3 3 3 2 2 2 2 2 2 2 2 2 2 +128
Exp=x^8
7777777777= 17 12 11 10 9 7 7 6 5 5 5 4 4 4 3 3 3 3 3 3 3 3 2 2 2 2 2 2 22 2 2 2 +33
Exp=x^9
7777777777= 12 11 8 7 7 7 5 5 4 4 4 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 +510
Exp=x^10
7777777777= 9 9 7 7 6 6 6 5 5 5 5 5 4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 3 22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 +502
Exp=x^11

Here is code for doing it for any exponent.

<HTML>
<HEAD><TITLE>TEST</TITLE></HEAD>
<SCRIPT language=Javascript>

function sq(number,exponent){
exp=1;
factor=2;
multip=Math.pow(2,exponent);
document.write(x,"= ");
while (number>multip){

while (exp<=number)
{ factor++;
exp=Math.pow(factor,exponent);

}
factor--;
document.write(factor," ");
exp=Math.pow(factor,exponent);
number=number-Math.pow(factor,exponent);
exp=1;
factor=1;
}
document.write("+",number,"<BR>");
}


//Set exponent by change y
y=10;
x=7777777777


for (y=2;y<20;y++){
document.write("Exp=x^",y,"<BR>");
sq(x,y);
}

</script>
<BODY>
</BODY>
</HTML>
 
D

Dave Angel

On 29/10/2013 14:35, (e-mail address removed) wrote:

(Deleting hundreds of quad-spaced garbage. Please be more considerate
of others if you choose to use buggy googlegroups, maybe starting by
studying:

)

Please indent by 4 columns, not 1. Since indentation is how scope is
specified in Python, it's very important to get it right.
i do not understand howto reach outer loop after finnish inner loop, in fact i do not understand when finished.

The inner loop is finished whenever you stop indenting by 8 columns. If
you have a fundamental problem like this, keep it simple till you
understand it:


q = 12
for x in range(10):
for y in range(3):
q = 3*q + 1
print("inner", q)
print("outer", x*q)

print("done")

Because of the detenting, the print("outer", x*q) is in the outer loop.
 
J

jonas.thornvall

Den tisdagen den 29:e oktober 2013 kl. 20:24:57 UTC+1 skrev Dave Angel:
On 29/10/2013 14:35, (e-mail address removed) wrote:



(Deleting hundreds of quad-spaced garbage. Please be more considerate

of others if you choose to use buggy googlegroups, maybe starting by

studying:



)



Please indent by 4 columns, not 1. Since indentation is how scope is

specified in Python, it's very important to get it right.






The inner loop is finished whenever you stop indenting by 8 columns. If

you have a fundamental problem like this, keep it simple till you

understand it:





q = 12

for x in range(10):

for y in range(3):

q = 3*q + 1

print("inner", q)

print("outer", x*q)



print("done")



Because of the detenting, the print("outer", x*q) is in the outer loop.


Why did Python not implement end... The end is really not necessary for theprogramming language it can be excluded, but it is a courtesy to the programmer and could easily be transformed to indents automaticly, that is removed before the compiliation/interpretation of code.

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?

#!/usr/bin/python
import math
# Function definition is here
def sq(number):

square=1;
factor=2;
exponent=2;
print(x,"= ");
while (number>3):
while (square<=number):
factor+=1;
square=math.pow(factor,exponent);
factor-=1;
print(factor,"^2");
square=math.pow(factor,exponent);
number=number-(factor*factor);
square=1;
factor=1;
print("+",number);
return

print("Exp=x^2");
for x in range (21,22):
sq(x);
 
J

jonas.thornvall

Den tisdagen den 29:e oktober 2013 kl. 21:08:39 UTC+1 skrev (e-mail address removed):
Den tisdagen den 29:e oktober 2013 kl. 20:24:57 UTC+1 skrev Dave Angel:






Why did Python not implement end... The end is really not necessary for the programming language it can be excluded, but it is a courtesy to the programmer and could easily be transformed to indents automaticly, that is removed before the compiliation/interpretation of code.



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 printcommand change line? Or must i build up a string/strings for later printout?



#!/usr/bin/python

import math

# Function definition is here

def sq(number):



square=1;

factor=2;

exponent=2;

print(x,"= ");

while (number>3):

while (square<=number):

factor+=1;

square=math.pow(factor,exponent);

factor-=1;

print(factor,"^2");

square=math.pow(factor,exponent);

number=number-(factor*factor);

square=1;

factor=1;

print("+",number);

return



print("Exp=x^2");

for x in range (21,22):

sq(x);

They could had used print and prinln from basic? I do not want new line everytime i write out some terms. And i do not like it add extra space after each print of variable print(factor,"^2") writes out 12 ^2 and i do not think there is any space after the factor?
 
N

Ned Batchelder

Why did Python not implement end... The end is really not necessary for the programming language it can be excluded, but it is a courtesy to the programmer and could easily be transformed to indents automaticly, that is removed before the compiliation/interpretation of code.

If you're going to indent your code correctly anyway, then the "end"s
are just extra noise. And if you aren't going to indent your code to
match the structure, then you have two different channels of
information: the human pays attention to the indentation, and the
computer pays attention to the ends. That's a recipe for creating lots
of subtle bugs. You get used to reading the indentation.
They could had used print and prinln from basic? I do not want new line everytime i write out some terms. And i do not like it add extra space after each print of variable print(factor,"^2") writes out 12 ^2 and i do not think there is any space after the factor?

The print statement is very simple, and has not had a lot of features
added to it, because you very quickly outgrow it anyway. If you want
fine control over the output of your program, you use string formatting,
for example with the .format() method on strings.

--Ned.
 
N

Neil Cerutti

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.
 
G

Gregory Ewing

Neil said:
Get in the habit of not using the semicolon to end lines.

Also, you don't need to put parentheses around the
conditions of while and if statements.
 
D

Dave Angel

Den tisdagen den 29:e oktober 2013 kl. 21:08:39 UTC+1 skrev (e-mail address removed):
They could had used print and prinln from basic? I do not want new line everytime i write out some terms.

That's ironic, when the bulk of your message is quad-spaced --- three
blank lines between every useful line. Please reread:

http://wiki.python.org/moin/GoogleGroupsPython


And i do not like it add extra space after each print of variable
print(factor,"^2") writes out 12 ^2 and i do not think there is any space after the factor?

Look up the print function, and see all the keyword options. Two in
particular:
sep=
end=

would address both your concerns.
 
T

Tim Roberts

Why did Python not implement end... The end is really not necessary for
the programming language it can be excluded, but it is a courtesy to
the programmer and could easily be transformed to indents automaticly,
that is removed before the compiliation/interpretation of code.

You only say that because your brain has been poisoned by languages that
require some kind of "end". It's not necessary, and it's extra typing. 99%
of programmers do the indentation anyway, to make the program easy to read,
so why not just make it part of the syntax? That way, you don't
accidentally have the indentation not match the syntax.
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 08:07:31 UTC+1 skrev Tim Roberts:
You only say that because your brain has been poisoned by languages that

require some kind of "end". It's not necessary, and it's extra typing. 99%

of programmers do the indentation anyway, to make the program easy to read,

so why not just make it part of the syntax? That way, you don't

accidentally have the indentation not match the syntax.

--

Tim Roberts, (e-mail address removed)

Providenza & Boekelheide, Inc.

It maybe common practice in program languages, but to me it is slightly confusing to have the while/for loop on the same indent level, as the regular statements.
Because when the while loop ends there is no other identification then that the indent stopped, and to me it is easy to interpret that terms that actually straight under the loop belongs to it.

But of course it is a matter of adjust the way i look at the code.
Thanks for help guys.
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 09:52:16 UTC+1 skrev (e-mail address removed):
Den onsdagen den 30:e oktober 2013 kl. 08:07:31 UTC+1 skrev Tim Roberts:




It maybe common practice in program languages, but to me it is slightly confusing to have the while/for loop on the same indent level, as the regular statements.

Because when the while loop ends there is no other identification then that the indent stopped, and to me it is easy to interpret that terms that actually straight under the loop belongs to it.



But of course it is a matter of adjust the way i look at the code.

Thanks for help guys.

To show you guys that i am not totally uneducable i actually followed your sugestions ;D.
(Is there any support similar javascript canvas for drawing, and HTML for interactivity, textbox, buttons in python?).

I have been programming some PHP long time ago, basicly only remember you had to have a server running to interact via HTML.

#!/usr/bin/python
import math
# Function definition is here
def sq(number):

square=1
factor=2
multip=exponent*exponent
print(x,"= ", end="")
while number>=multip:
while square<=number:
factor+=1
square=factor*factor
factor-=1
print(factor,"^2+",sep="",end="")
square=factor*factor
number=number-(factor*factor)
square=1
factor=1
print(number)

#Set exponent here
exponent=3
print("Exp=x^",exponent,sep="")
#Set range of numbers x
for x in range (1,100):
sq(x);
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 10:48:36 UTC+1 skrev (e-mail address removed):
Den onsdagen den 30:e oktober 2013 kl. 09:52:16 UTC+1 skrev (e-mail address removed):




To show you guys that i am not totally uneducable i actually followed your sugestions ;D.

(Is there any support similar javascript canvas for drawing, and HTML for interactivity, textbox, buttons in python?).



I have been programming some PHP long time ago, basicly only remember you had to have a server running to interact via HTML.



#!/usr/bin/python

import math

# Function definition is here

def sq(number):



square=1

factor=2

multip=exponent*exponent

print(x,"= ", end="")

while number>=multip:

while square<=number:

factor+=1

square=factor*factor

factor-=1

print(factor,"^2+",sep="",end="")

square=factor*factor

number=number-(factor*factor)

square=1

factor=1

print(number)



#Set exponent here

exponent=3

print("Exp=x^",exponent,sep="")

#Set range of numbers x

for x in range (1,100):

sq(x);

Forgot change the static square that was written out, i am a bit undecuable afterall...

#!/usr/bin/python
import math
# Function definition is here
def sq(number):

square=1
factor=2
multip=exponent*exponent
print(x,"= ", end="")
while number>=multip:
while square<=number:
factor+=1
square=factor*factor
factor-=1
print(factor,"^",exponent,"+",sep="",end="")
square=factor*factor
number=number-(factor*factor)
square=1
factor=1
print(number)

#Set exponent here
exponent=3
print("Exp=x^",exponent,sep="")
#Set range of numbers x
for x in range (1,100):
sq(x);
 
M

Mark Lawrence

On 30/10/2013 09:52, (e-mail address removed) wrote:

Please stop sending us double spaced crap.
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 08:07:31 UTC+1 skrev Tim Roberts:
You only say that because your brain has been poisoned by languages that

require some kind of "end". It's not necessary, and it's extra typing. 99%

of programmers do the indentation anyway, to make the program easy to read,

so why not just make it part of the syntax? That way, you don't

accidentally have the indentation not match the syntax.

--

Tim Roberts, (e-mail address removed)

Providenza & Boekelheide, Inc.

Well Tim ***one could argue*** why not do a (i think it is called parser) that react to "loop", "end" and "function". And lazy like me do not have to think about "what is not part of program".

I certainly do not like the old bracket style it was a catastrophe, but in honesty the gui editor of python should have what i propose, a parser that indent automaticly at loops, functions and end. I promise you it will save millions of hours of bug searching all over world in a month.
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 11:08:11 UTC+1 skrev (e-mail address removed):
Den onsdagen den 30:e oktober 2013 kl. 08:07:31 UTC+1 skrev Tim Roberts:




Well Tim ***one could argue*** why not do a (i think it is called parser)that react to "loop", "end" and "function". And lazy like me do not have to think about "what is not part of program".



I certainly do not like the old bracket style it was a catastrophe, but in honesty the gui editor of python should have what i propose, a parser that indent automaticly at loops, functions and end. I promise you it will save millions of hours of bug searching all over world in a month.

Instead of having going over indent manually, you just drop in an end it isso simple, no marking no meny indent unindent it is automaticly done. And that was the purpose of python to remove idiocies, like brackets and indents.
 
J

jonas.thornvall

Den onsdagen den 30:e oktober 2013 kl. 11:00:30 UTC+1 skrev Mark Lawrence:
On 30/10/2013 09:52, (e-mail address removed) wrote:



Please stop sending us double spaced crap.



--

Python is the second best programming language in the world.

But the best has yet to be invented. Christian Tismer



Mark Lawrence
I am not sure what you want.
You want me to remove the empty line in function? I do it for it is easier to read for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top