0

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)
Community
  • 1
  • 1
B0neCh3wer
  • 1
  • 1
  • 6
  • 1
    It is not clear exactly what you want to do here. Can you give some sample input and expected output? – Hannes Ovrén Nov 25 '16 at 11:49
  • `enumerate(sentence.split())` – khelwood Nov 25 '16 at 11:50
  • So i'll just give you a simple run through of what it does so first the user enters a sentence, then the program prints this sentence and converts the words to numbers, but in the position it is in on the list, so for example i enter "Hello world" the code should print "1 2" – B0neCh3wer Nov 25 '16 at 11:53

2 Answers2

1

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(' ')):
Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
0

You can also try this:

>>> sentence = 'I like Moive'
>>> sentence = sentence.lower()
>>> sentence = sentence.split()
>>> for i, j in enumerate(sentence):
...         print(i, j)
Ashok Kumar Jayaraman
  • 2,887
  • 2
  • 32
  • 40