Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with List Loops
#1
I'm working through an assignment and am having some difficulty. Code below

listToPrint=['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
print ('Your list consists of ' + str(listToPrint[0:-1]) + ' and ' + str(listToPrint[-1]) +'.')
break
else:
print ('Your lists consists of ' + str(listToPrint[0]) + '.')

Output is supposed to be:
Your list consists of dog, cat, hamster, and pig. #Note the commas and the and in the sentence.

Instead I'm getting:
Your list consists of ['dog', 'cat', 'hamster'] and pig.

I can't figure out two things. How do i get rid of the brackets and add the commas. Any help is appreciated.

I'm working through an assignment and am having some difficulty. Code below
listToPrint=['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
     print ('Your list consists of ' + str(listToPrint[0:-1]) + ' and ' + str(listToPrint[-1]) +'.')
     break
else:
     print ('Your lists consists of ' + str(listToPrint[0]) + '.')
Output is supposed to be:
Your list consists of dog, cat, hamster, and pig. #Note the commas and the and in the sentence.

Instead I'm getting:
Your list consists of ['dog', 'cat', 'hamster'] and pig.

I can't figure out two things. How do i get rid of the brackets and add the commas. Any help is appreciated.
Reply
#2
Try print(', '.join(list_to_print)).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you Craig....and thanks for making me work for it.

You gave me the code snippet but I still had to manipulate it a little get it the way I needed.

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)

while len(listToPrint) > 1:
    print ('Your list consists of ' + (', '.join(listToPrint[0:-1]) + ', and ' + str(listToPrint[-1]) +'.'))
    break
else:
    print ('Your lists consists of ' + str(listToPrint[0]) + '.')
Reply
#4
What I recommend you and you need to do is to add some for loops in your while loop . in order to get indivisual items in your list .
this code below can probably fix your program , but that's not the only way .
listToPrint = ['dog', 'cat', 'hamster', 'pig']

while len(listToPrint) > 1:
    print("Your list consists of : ")
    for i in listToPrint:
        print(i)
    break
else:
    print('Your lists consists of ' + str(listToPrint[0]) + '.')
If you are uncomfortable using this method , I can also show you some diffrent ways of doing it .
Reply
#5
ichabod801 gave very strong hint how to achieve desired result.

>>> lst = ['dog', 'cat', 'hamster', 'pig']
>>> f'Your list consists {", ".join(lst[:-1])}, and {lst[-1]}.'
'Your list consists dog, cat, hamster, and pig.'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 882 Sep-18-2023, 07:52 AM
Last Post: buran
  Using recursion instead of for loops / list comprehension Drone4four 4 3,135 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Adding items in a list (using loops?) Seneca260 6 2,748 Nov-22-2019, 11:34 AM
Last Post: Seneca260
  Loops in List SandraDan 19 12,455 Mar-21-2017, 02:34 AM
Last Post: nilamo
  simple list check with loops Low_Ki_ 23 15,329 Jan-09-2017, 03:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020