Nov-24-2019, 08:11 PM
(This post was last modified: Nov-24-2019, 08:42 PM by slackerman73.)
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
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.
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
1 2 3 4 5 6 7 |
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 ]) + '.' ) |
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.