将9个连续变量合并为1个变量b

2021-10-17 14:33:32 发布

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

我在下面有九个变量和一个函数,但是我把重点放在这个问题的变量部分。(此代码的前九行。)

groceryfruits1 = 'The number of fruit(s) in the grocery bag is: 1.'
groceryfruits2 = 'The number of fruit(s) in the grocery bag is: 2.'
groceryfruits3 = 'The number of fruit(s) in the grocery bag is: 3.'
groceryfruits4 = 'The number of fruit(s) in the grocery bag is: 4.'
groceryfruits5 = 'The number of fruit(s) in the grocery bag is: 5.'
groceryfruits6 = 'The number of fruit(s) in the grocery bag is: 6.'
groceryfruits7 = 'The number of fruit(s) in the grocery bag is: 7.'
groceryfruits8 = 'The number of fruit(s) in the grocery bag is: 8.'
groceryfruits9 = 'The number of fruit(s) in the grocery bag is: 9.'

def checkout(itemcount, category):
    if category == "fruits":
        if itemcount == 1:
            print groceryfruits1
        elif itemcount == 2:
            print groceryfruits2
        elif itemcount == 3:
            print groceryfruits3
        elif itemcount == 4:
            print groceryfruits4
        elif itemcount == 5:
            print groceryfruits5
        elif itemcount == 6:
            print groceryfruits6
        elif itemcount == 7:
            print groceryfruits7
        elif itemcount == 8:
            print groceryfruits8
        elif itemcount == 9:
            print groceryfruits9

checkout(9, "fruits")

既然有一个连续的变量列表,而且所有的变量都在一行中会更加有序,那么有什么方法可以做到这一点吗?你知道吗

3条回答
网友
1楼 ·

一般来说,一组名称相似的变量应该合并到一个列表中。(对于这一点,我们将忽略这些值本身是相似的,而其他答案则更优。)

groceryfruits = [
    'The number of fruit(s) in the grocery bag is: 1.',
    'The number of fruit(s) in the grocery bag is: 2.',
    'The number of fruit(s) in the grocery bag is: 3.',
    'The number of fruit(s) in the grocery bag is: 4.',
    'The number of fruit(s) in the grocery bag is: 5.',
    'The number of fruit(s) in the grocery bag is: 6.',
    'The number of fruit(s) in the grocery bag is: 7.',
    'The number of fruit(s) in the grocery bag is: 8.',
    'The number of fruit(s) in the grocery bag is: 9.'
]

def checkout(itemcount, category):
    if category == "fruits":
        if 1 <= itemcount <= 9:
            # List indices start at 0
            print groceryfruits[i-1]


checkout(9, "fruits")
网友
2楼 ·

您可以将代码简化为:

groceryfruits = 'The number of fruit(s) in the grocery bag is: {}.'

def checkout(itemcount, category):
    if category == "fruits":
        print groceryfruits.format(itemcount)

checkout(9, "fruits")
网友
3楼 ·

水果计数之前的字符串总是相同的,所以为什么不将其编码到函数中呢?你知道吗

def checkout(itemcount, category):
    if category == 'fruits':
        print 'The number of fruit(s) in the grocery bag is: {0}.'.format(itemcount)

一旦有了更多的项目类别,您可能需要考虑像这样(或类似)编写函数,以获得更大的灵活性:

def item_checkout(itemcount, category):
    print 'the number of {0} items in the grocery bag is: {1}'.format(category, itemcount)

或者,如果您想要一个通用的checkout函数,让它使用(itemcount, category)元组列表:

def total_checkout(items):
    'items: list of (itemcount, category) tuples'
    for itemcount, category in items:
        print 'the number of {0} items in the grocery bag is: {1}'.format(category, itemcount)

演示:

>>> total_checkout([(5, 'banana'), (2, 'fruit'), (7, 'sirup')])
the number of banana items in the grocery bag is: 5
the number of fruit items in the grocery bag is: 2
the number of sirup items in the grocery bag is: 7

相关问题