Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Recursive Functions Alternatives?

Code
def examplefunction1(n)
     print "You have " + str(n) + " tiny numbers!"


def examplefunction2()
     random = random.random()
     if random < 2:
           return random
     else:
           examplefunction2


examplefunction1(examplefunction2())

Recently started learning Python, and I'm having a bit of confusion over so called recursive functions.
Basically, if I had a recursive function, would it send the return back to its original caller? If not, what can I do to replace that?


Uhh, is anyone there?


Anyone?
PHP Code

<?php 
from random import randint
def examplefunction1
(n):
 
    print("You have " + str(n) + " tiny numbers!")


def examplefunction2():
 
    random = randint(0,10)
 
    print(random)
 
    if random < 2:
 
          return random
     
else:
 
          return examplefunction2()
examplefunction1(examplefunction2()) 

Your code wouldn't actually work and I added a print statement so you can it rolling numbers

output example
PHP Code

<?php 
3
9
10
1
You have 1 tiny numbers
! 

when the number is more than 2 is returns the function call again and again until it is less than two in which case all the returns trigger and you get your result.

Your other options are to use loops.

PHP Code

<?php 
from random import randint
def examplefunction1
(n):
 
    print("You have " + str(n) + " tiny numbers!")


def examplefunction2():
 
    random = randint(0,10)
 
    while(random >2):
 
      random = randint(0,10)
 
      print(random)
 
    return random


examplefunction1
(examplefunction2()) 

¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SIM-3437924Aug 15, 2020 09:57 AM 1 185 Aug 15, 2020 09:57 AM
Last Post: SIM-3437924
[C] by Norbi34Feb 26, 2020 11:33 AM 3 181 Feb 26, 2020 11:33 AM
Last Post: saltycracker
[C] by hansbaldMay 29, 2018 07:50 PM 12 958 May 29, 2018 07:50 PM
Last Post: XMR Miner