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.

4

3 回答 3

0

您的程序中确实不需要循环,只需拼出函数调用即可。

如果你无论如何都想要一个循环,你可以使用这样的东西,尽管它非常没有意义:

def primer():
    for s in ([greet(), intro(), origi()][:i + 1] for i in range(3)):
        print('\n'.join (s) + '\n')
于 2013-10-21T00:16:04.363 回答
0

通过对代码进行最少的更改,您可以通过在调用下一个函数时打印前一个函数的返回值来做您想做的事情:

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()

给我:

>>> 
Hola 
Hola 
Mi nombre es Leon 
Hola  
 Mi nombre es Leon  
 I am from Guadalajara
于 2013-10-21T00:16:06.003 回答
0

这应该适用于返回字符串的任意函数列表 ( printers):

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)]))

输出:

Hola 



Hola 
Mi nombre es Leon 



Hola 
Mi nombre es Leon 
I am from Guadalajara
于 2013-10-21T00:17:39.163 回答