简体   繁体   中英

How to return a list on different lines in python?

Right now, my codes output is ['I love mac and cheese', ' cheese', 'se'], how would I change it so that the output would be a string and appear on different lines so "I love mac and cheese (first line) cheese (second line) se (third line). This is the output when the user inputs.369 for fraction and I love mac and cheese for sentence

def echo(a,b, count):
    newlen = int(len(b)*a)
    if newlen > 0:
        count+=1
        return [b] + echo(a,b[-newlen:], count)
    else:
        count+=1
        print("Number of echos: ", count)
        return [b]
        
def main():
    count=0
    f=float(input("Enter the fraction"))
    sentence=input("Enter the sentence")
    print(echo(f,sentence, count))


main()
    

Try the following. Assuming you have successfully achieved the right sentence...

sentence = ["I love mac and cheese", "cheese", "se"]
for item in sentence:
    print(item)
    # If you want a newline character between each item:
    # print("\n") \n is the python syntax for a newline

You can simply assign the return of your echo function to a list, then iterate over that list printing the elements.

def main():
    count = 0
    f = float(input("Enter the fraction"))
    sentence = input("Enter the sentence")
    results = echo(f, sentence, count)
    for line in results:
        print(line)

Results in:

Enter the fraction.369
Enter the sentenceI love mac and cheese
Number of echos:  3
I love mac and cheese
 cheese
se

You can store the list returned by the echo method in a variable and use a for loop to print out the contents separately.

results=echo(f,sentence, count)

for result in results:
    print(result)

Use the str.join() method with new-line.

>>> a = ['some text', 'some other text', 'some more']
>>> print(a)
['some text', 'some other text', 'some more']
>>> print('\n'.join(a))
some text
some other text
some more
>>> 


Got it, I think.

So, the key is to is to understand when you are returning back to the outside world, ie to print. The item to watch is count , because it's at 1 right before that.

def echo(frac,b, count):
    newlen = int(len(b)*frac)
    if newlen > 0:
        count+=1
        #`count` is going to go up for the inner, recursive, calls
        res = [b] + echo(frac,b[-newlen:], count)

        if count == 1:
            #back to the caller.
            return "\n".join(res)
        else:
            #nope, this a recursive call, leave as list of strings.
            return res
    else:
        count+=1
        print("Number of echos: ", count)
        if count == 1:
            return b
        else:
            return [b]

        
def main():
    count=0
    f= .369 # float(input("Enter the fraction"))
    sentence="I love mac and cheese" #input("Enter the sentence")
    print(echo(f,sentence, count))


main()

output:

Number of echos:  3
I love mac and cheese
 cheese
se

In a way, it helps if instead of count you think of as is as level or depth and understand that it tracks how deep in the recursions you are. Nice question - I always struggle with recursion exits and this is a nice trick to know, tracking levels.

Also, if you had a short sentence or a very small fraction, you might not hit newlen > 0 even once. Your code should allow for that in your else branch and return the "\n".join([b]) .

without having to recurse because of small fraction:

    f= .01 # float(input("Enter the fraction"))
    sentence="I love mac and cheese" #input("Enter the sentence")

output:

Number of echos:  1
I love mac and cheese

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM