Dragonborn_Dyfed Dragonborn_Dyfed - 1 year ago 102
Python Question

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()
for i,j in enumerate(sentence):
print (i,j)

Answer Source

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(' ')):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download