Working with Lists


#1

Hello everyone! I am trying to create a program that allows a user to enter a list and then prints out the list they entered with each value containing a comma and the last two list items separated with the word 'and'. The user should be able to keep adding items to the list and the list should then be printed out again continuously updated.

For example, if the list was [dog, cat, monkey, rabbit] it would print out "dog, cat, monkey and rabbit". The user should then be able to add a new item such as "squirrels, bird" and it would update and print out "dog, cat, monkey, rabbit, squirrel and bird"

listToPrint = []
while True:
    newWord = input("Enter a word to add to the list (press return to stop adding words) > ")
    if newWord == "":
      break
    else:
      listToPrint.append(newWord)
      print(listToPrint)

I can't seem to get "and" to show up between the last two or list items to enter properly

"
Enter a word to add to the list (press return to stop adding words) > dogs
['dogs']
Enter a word to add to the list (press return to stop adding words) > cats
['dogs', 'cats']
Enter a word to add to the list (press return to stop adding words) > apples, bananas
['dogs', 'cats', 'apples, bananas']
Enter a word to add to the list (press return to stop adding words) >
"

or

"
Enter a word to add to the list (press return to stop adding words) > dogs
['dogs']
Enter a word to add to the list (press return to stop adding words) > cats
['dogs', 'cats']
Enter a word to add to the list (press return to stop adding words) > badgers
['dogs', 'cats', 'badgers']
Enter a word to add to the list (press return to stop adding words) > rabbit
['dogs', 'cats', 'badgers', 'rabbit']
Enter a word to add to the list (press return to stop adding words) >
"

Thank you for any and all help!


#2

Join the last two words on ' and ', then join everything on ', '

['dogs', 'cats', 'badgers', 'rabbit']
['dogs', 'cats', 'badgers and rabbit']
'dogs, cats, badgers and rabbit'

With a special case for when there's less than two words

To get an oxford comma you would prepend ' and ' to the last word (except for when there's less than two words)


#3

Hey ionatan, thanks for your reply! I have control over that? In this case we are starting out with an empty list and allowing the user to enter their own list in the test console. Where in my code should I be looking to make these edits?
Thanks again!


#4

Code describes actions to carry out, you'd put it in the same place as you would if the description was in English. You just have to repeatedly decide on what to do and the find out how to do it, I believe I just did a chunk of that, by coming up with a series of steps that add up to the desired result


#5

Thanks again for you help! Trying to make sense of this has me so confused, but I'll keep trying to figure this all out.... I do really appreciate you trying to help but I'm still so new to this that that last reply almost has me more confused! haha