0

I want to inter-loop print, I don't know if that is even a word but let me demonstrate with the code bellow

def primer():
    print (greet(), "\n", intro(), "\n" ,origi())

def greet():
    return("Hola ")

def intro():
    return("Mi nombre es Leon ")

def origi():
    return("I am from Guadalajara")

primer()

the output is:

Hola  
 Mi nombre es Leon  
 I am from Guadalajara

Desired output.

Hola



Hola
 Mi nombre es Leon



Hola
 Mi nombre es Leon
 I am from Guadalajara

That would be to pirint

greet

greet
intro

greet
intro
origi 

Without all the redundancy or as little as possible.

  • Could you describe more or less how your desired output is related to your function definitions? It is a bit complicated to guess. – Hyperboreus Oct 21 '13 at 00:11

3 Answers3

0

There really is no need for a loop in your program, just spell out the function calls.

If you by all means want a loop, you can use something like this although it is highly senseless:

def primer():
    for s in ([greet(), intro(), origi()][:i + 1] for i in range(3)):
        print('\n'.join (s) + '\n')
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
0

With the minimum of alteration to your code, you can do what you want by printing the previous function's return value when the next is called:

def primer():
    print (greet(), "\n", intro(), "\n" ,origi())

def greet():
    return("Hola ")

def intro():
    print(greet())
    return("Mi nombre es Leon ")

def origi():
    print(intro())
    return("I am from Guadalajara")

primer()

Gives me:

>>> 
Hola 
Hola 
Mi nombre es Leon 
Hola  
 Mi nombre es Leon  
 I am from Guadalajara
Pines
  • 396
  • 1
  • 8
0

This should work for any arbitrary list of functions (printers) that return strings:

def primer():
    printers = (greet, intro, origi)
    print('\n\n\n\n'.join(['\n'.join([printer() for printer in printers[1:n]]) for n in range(len(printers)+1)]))

OUTPUT:

Hola 



Hola 
Mi nombre es Leon 



Hola 
Mi nombre es Leon 
I am from Guadalajara
Brionius
  • 13,858
  • 3
  • 38
  • 49