Search code examples
pythonenumerate

How do I use Enumerate to to make each word into a number not each character


So I need to make a program that gets the user to enter a sentence, and then the code turns that sentence into numbers corresponding to it's position in the list, I cam across the command Enumerate here: Python using enumerate inside list comprehension but this gets every character not every word, so this is my code so far, can anyone help me fix this?

list = []
lists = ""
sentence= input("Enter a sentence").lower()
print(sentence)
list.append(lists)
print(lists)
for i,j in enumerate(sentence):
    print (i,j)

Solution

  • Your sentence is string, so it is split to single chars. You should split it to words first:

    for i,j in enumerate(sentence.split(' ')):