Joining words together with a comma, and “and”

后端 未结 6 1661
Happy的楠姐
Happy的楠姐 2020-12-10 02:10

I\'m working through \'Automate the Boring Stuff with Python\'. I can\'t figure out how to remove the final output comma from the program below. The goal is to keep promptin

6条回答
  •  感动是毒
    2020-12-10 02:26

    Modifying your code a little bit...

    def lister():
        listed = []
        while True:
            print('type what you want to be listed or type nothing to exit')
            inputted = input()
            if inputted == '':
                break
            else:
                listed.append(inputted) # removed the comma here
    
        print(', '.join(listed[:-2]) + ' and ' + listed[-1])  #using the join operator, and appending and xxx at the end
    lister()
    

提交回复
热议问题