Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 11th, 2012, 09:38 AM
 klskl
Registered User

Join Date: Sep 2012
Posts: 10
Time spent in forums: 1 h 35 m 12 sec
Reputation Power: 0
What happends here?- function

Hello, I just have a quick question regarding a very simple function, what happends?

Code:
```def function():
z=0
for i in range(0,5):
for j in range(1,4):
z+=j
print(z)
function()```

#2
December 11th, 2012, 09:50 AM
 rrashkin
Contributing User

Join Date: May 2012
Location: 39N 104.28W
Posts: 139
Time spent in forums: 2 Days 15 m 51 sec
Reputation Power: 2
Quote:
 Originally Posted by klskl Hello, I just have a quick question regarding a very simple function, what happends? Code: ```def function(): z=0 for i in range(0,5): for j in range(1,4): z+=j print(z) function()```

Well, the way you posted it, nothing would happen because the indentation is screwed up. What it appears to be doing is adding 1+2+3 5 times, that is at the end, z=30.

#3
December 11th, 2012, 10:13 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
fix post

You get an IndentationError . Please read the information at my signature to post code.

Code:
```def function():
z=0
for i in range(0,5):
print('i has value %d'%i)
for j in range(1,4):
print('\tj has value %d'%j)
z+=j
print('\tz gruesome, %d'%z)
print(z)

function()```
__________________
[code]Code tags[/code] are essential for python code!

#4
December 11th, 2012, 11:42 AM
 klskl
Registered User

Join Date: Sep 2012
Posts: 10
Time spent in forums: 1 h 35 m 12 sec
Reputation Power: 0
Quote:
 Originally Posted by rrashkin Well, the way you posted it, nothing would happen because the indentation is screwed up. What it appears to be doing is adding 1+2+3 5 times, that is at the end, z=30.

yes, but why does it do that? How can you see that, if you care to explain I would be grateful

#5
December 11th, 2012, 11:48 AM
 rrashkin
Contributing User

Join Date: May 2012
Location: 39N 104.28W
Posts: 139
Time spent in forums: 2 Days 15 m 51 sec
Reputation Power: 2
First you set z=0.
The inner-most loop, for j in range(1,4):, makes j=1,2,3 in succession. At each iteration j is added to z and the sum is the new z, z+=j.
You do that 5 times: for i in range(0,5):, where i= 0,1,2,3,4 but is just a loop counter. That is, i is not used in the calculation.

#6
December 11th, 2012, 03:53 PM
 klskl
Registered User

Join Date: Sep 2012
Posts: 10
Time spent in forums: 1 h 35 m 12 sec
Reputation Power: 0
Quote:
 Originally Posted by rrashkin First you set z=0. The inner-most loop, for j in range(1,4):, makes j=1,2,3 in succession. At each iteration j is added to z and the sum is the new z, z+=j. You do that 5 times: for i in range(0,5):, where i= 0,1,2,3,4 but is just a loop counter. That is, i is not used in the calculation.

Thanks!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > What happends here?- function