Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-11-2013, 12:15 PM   #1
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Python: can't figure this out?

This prints:

# 1-Python is fun
# 2-Python is fun
# 3-Python is fun
# 4-Python is fun
# 5-Python is fun

I understand why it prints in reverse order but i'm confused of why it includes (# 5-Python is fun) since it seems to subtract 1 from 5 before it prints. The only thing I can think of is that once if finds num1 to be >= to 1 to be TRUE it goes strait to print then loops back up to subtract then test again, I remember reading about the "counterclockwise" motion in the "Think Python" book.

def moose(num1, str1):
if num1 >= 1:
moose(num1-1, str1)
print "# %d-%s" % (num1, str1)
else:
return

moose(5, "Python is fun")
__________________

palermo is offline   Reply With Quote
Old 05-12-2013, 03:52 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Python: can't figure this out?

You may want to look up a bit on recursion, since what's happening here appears to be a lack of understand on this concept rather than python in particular.

Note that whenever you call moose, assuming num1 is >=1, it first calls moose with a number 1 less than num1, and *then* prints out its value (without 1 being subtracted.) The key point is that it doesn't subtract 1 from num1 and then store it back in num1, it just subtracts it and passes it to the next moose method. Once that line is over (which due to recursion in this case involves all other numbers being printed out), it goes onto execute the "print" line with the current value of num1, which hasn't changed, so it prints out a 5.

Whatever you do, get out of your head that it somehow jumps to print and then back up again - pretty much all programming languages, including python, will follow a logical progression from line to line unless otherwise stated (for instance when you reach the end of a loop, a break statement, return statement or similar.) If you have to resort to thinking loops work backwards or something weird like that, you've probably not understood it properly
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 05-12-2013, 03:56 PM   #3
Baseband Member
 
palermo's Avatar
 
Join Date: Jan 2013
Location: United States
Posts: 81
Default Re: Python: can't figure this out?

Thank you!
palermo is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:50 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0