Automate the boring stuff with Python: Comma Code

前端 未结 29 1324
深忆病人
深忆病人 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:41

    That one wins for simplicity Hein.

    Only, the author specified:

    "your function should be able to work with any list value passed to it."

    To accompany non strings, add str() tags to al the [i] functions.

    spam = ['apples', 'bananas', 'tofu', 'cats', 'bears', 21]
    def pList(x):
        for i in range(len(x) - 2):
            print(str(x[i]) + ', ', end='')
        print(str(x[-2]) + ' and ' + str(x[-1]))
    pList(spam)
    

提交回复
热议问题