我需要创建一个函数,该函数传递多个列表并返回一个字符串,然后进行打印

2024-06-06 23:08:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个函数,该函数传递多个列表并返回一个字符串,然后进行打印。老实说,我甚至不知道我是否朝着正确的方向前进

wordlist = ['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes'],
['subtract', 'add'],
['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above'],
['question'],
[]


def createSentence(wordlist):
    if len(wordlist) > 1:
        return 'The ' + str(len(wordlist)) + ' sight words for this week are ' + wordlist + '.'
    elif len(wordlist) == 1:
        return 'The only sight word for this week is' + wordlist + '.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

print(createSentence(wordlist))

而且我认为我的清单应该是这样的

week2 = ['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes']
week5 = ['subtract', 'add'] 
week10 = ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above']
week13 = ['question']
week17 = []

但我不知道如何将它们传递给函数


Tags: 函数onlynewforlenreturnthisweek
3条回答

我认为您可能希望使用join函数将列表转换为字符串

(“,”).join(单词列表)

week2 = ['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes']
week5 = ['subtract', 'add'] 
week10 = ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above']
week13 = ['question']
week17 = []


def createSentence(wordlist):
    if len(wordlist) > 1:
        return 'The ' + str(len(wordlist)) + ' sight words for this week are ' + (' ,').join(wordlist)  + '.'
    elif len(wordlist) == 1:
        return 'The only sight word for this week is' + (' ,').join(wordlist) + '.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

第2周的输出: 'The 7 sight words for this week are new ,barn ,shark ,hold ,art ,only ,eyes.'

我建议采取不同的方法,为您的数据创建一个数据结构(在本例中为字典):

wordlists = {
    'week 2': ['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes'],
    'week 5': ['subtract', 'add'],
    'week 10': ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above'],
    'week 13': ['question'],
    'week 17': [],
}

def createSentence(week):
        wordlist = wordlists[week]

        length = len(wordlist)

        if length > 1:
            return "The {} sight words for {} are: {}.".format(length, week, ", ".join(wordlist))

        if length == 1:
            return "The only sight word for {} is: {}.".format(week, ", ".join(wordlist))

        return "There are no new sight words for {}!".format(week)

for week in wordlists:
    print(createSentence(week))

输出

> python3 test.py
The 7 sight words for week 2 are: new, barn, shark, hold, art, only, eyes.
The 2 sight words for week 5 are: subtract, add.
The 9 sight words for week 10 are: girl, house, best, thing, easy, wrong, right, again, above.
The only sight word for week 13 is: question.
There are no new sight words for week 17!
> 

像这样:

wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes'],
            ['subtract', 'add'],
            ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above'],
            ['question'],
            []]


def createSentence(wordlist):
    if len(wordlist) > 1:
        return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist)}.'
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

for lst in wordlist:
    print(createSentence(lst))

输出:

The 7 sight words for this week are new, barn, shark, hold, art, only, eyes.
The 2 sight words for this week are subtract, add.
The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again, above.
The only sight word for this week is question.
There are no new sight words for this week!

相关问题 更多 >