Home > Back-end >  From a function, how do I return a list whose items are on different lines?
From a function, how do I return a list whose items are on different lines?

Time:08-16

Take a look eat the list below

nursery_rhyme = [
'This is the house that Jack built.',
'This is the malt that lay in the house that Jack built.',
'This is the rat that ate the malt that lay in the house that Jack built.',
'This is the cat that killed the rat that ate the malt that lay in the house that Jack built.'
]

The function below is supposed to return the lines of the list above within the specified range

def recite(start_verse, end_verse):
    lines = []
    start = start_verse
    lines.append(nursery_rhyme[(start-1)])
    while start < end_verse:
        start  = 1
        line = nursery_rhyme[start-1]
        lines.append(line)
    return (lines)

In use:

print(recite(1,2))

Output:

['This is the house that Jack built.', 'This is the malt that lay in the house that Jack built.']

How do I get my output to look like this:

[
 'This is the house that Jack built.',
 'This is the malt that lay in the house that Jack built.'
]

CodePudding user response:

you may try it like this...

def recite(start_verse, end_verse):
  for line in (start_verse-1, end_verse):
    print(nursery_rhyme[line])


recite(1,2)

CodePudding user response:

Instead of printing whole list at once you can loop through each string and print it out:

for i in recite(1, 2):
    print(i)
  • Related