0

I want to assign numbers to words (starting from one) and create two lists, one of the words, one of the numbers. Then create a file and store both lists in it. So far I have:

sentence = input('Please enter a sentence: ')
list_of_words = sentence.split()
words_with_numbers = enumerate(list_of_words, start=1)
ForceBru
  • 43,482
  • 10
  • 63
  • 98

2 Answers2

0

Just open a file and write to it

In [1]: sentence = input('Please enter a sentence: ')
Please enter a sentence: Hello World

In [2]: with open('output.txt', 'w') as f:
   ...:     for i, word in enumerate(sentence.split(), start=1):
   ...:         f.write("{} {}\n".format(i, word))
   ...:

Then

$ cat output.txt
1 Hello
2 World
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

What you can try (if this is what you want) is:

sentence = input('Please enter a sentence: ')
list_of_words = sentence.split()
words_with_numbers = enumerate(list_of_words, start=1)
filename = 'yourfilename.txt'
with open(filename, 'w+') as file:
    file.write(str(list_of_words) + '\n' + str(words_with_numbers) + '\n')
Nf4r
  • 1,390
  • 1
  • 7
  • 9