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:
I am working through the same book and came up with this solution: This allows the user to input some values and create a list from the input.
userinput = input('Enter list items separated by a space.\n')
userlist = userinput.split()
def mylist(somelist):
for i in range(len(somelist)-2): # Loop through the list up until the second from last element and add a comma
print(somelist[i] + ', ', end='')
print(somelist[-2] + ' and ' + somelist[-1]) # Add the last two elements of the list with 'and' in-between them
mylist(userlist)
Example:
user input: one two three four five Output: one, two, three, four and five