In: Computer Science
For Python, I cannot figure out why "and" is printing at the beginning of my list. Can someone help?
listToPrint = []
while True:
newWord = input("Enter a word to add to the list (prest return to stop adding words) > ")
newWord == "":
break
else:
listToPrint.append(newWord)
for item in listToPrint[:-1]:
print(item, end=', ')
print('and', listToPrint[-1])
If I enter m, n, b to the list, it executes to
and m, n, b
Probably it is due to indentation issue in your for loop:-
Here is a code working properly -> Make sure to use proper indentation
Code is Given Below:
====================
listToPrint = []
while True:
newWord = input("Enter a word to add to the list (prest return to
stop adding words) > ")
if newWord == "":
break
else:
listToPrint.append(newWord)
for item in listToPrint[:-1]:
print(item, end=', ')
print('and', listToPrint[-1])
Output:
=============
Code Snapshot:
===================