Automate the boring stuff with Python: Comma Code

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

    The format statement is cleaner.

    This worked for me as well:

    def sentence(x):
        if len(x) == 1:
            return x[0]
        return (', '.join(x[:-1])+ ' and ' + x[-1])
    

提交回复
热议问题