Automate the boring stuff with Python: Comma Code

前端 未结 29 970
深忆病人
深忆病人 2021-02-03 16:17

Currently working my way through this beginners book and have completed one of the practice projects \'Comma Code\' which asks the user to construct a program which:

29条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-03 16:58

    Just a simple code. I think we don't need to use any fancy stuff here. :p

    def getList(list):
        value = ''
        for i in range(len(list)):
            if i == len(list) - 1:
                value += 'and '+list[i]
            else:
                value += list[i] + ', '
        return value
    
    spam = ['apples', 'bananas', 'tofu', 'cats']
    
    print('### TEST ###')
    print(getList(spam))
    

提交回复
热议问题