Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 11th, 2012, 08:38 AM
klskl klskl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 10 klskl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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()

Reply With Quote
  #2  
Old December 11th, 2012, 08:50 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 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.

Reply With Quote
  #3  
Old December 11th, 2012, 09:13 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,352 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 49 m 23 sec
Reputation Power: 383
fix post

You get an IndentationError . Please read the information at my signature to post code.
(use code tags instead of[indent].)

Rashkin was already here!

You can help yourself with print statements:
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!

Reply With Quote
  #4  
Old December 11th, 2012, 10:42 AM
klskl klskl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 10 klskl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old December 11th, 2012, 10:48 AM
rrashkin's Avatar
rrashkin rrashkin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: 39N 104.28W
Posts: 90 rrashkin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 37 m 44 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.

Reply With Quote
  #6  
Old December 11th, 2012, 02:53 PM
klskl klskl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 10 klskl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > What happends here?- function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap