Here is my code -
sentence = input("Enter a sentence without punctuation")
sentence = sentence.lower()
words = sentence.split()
pos = [words.index(s)+1 for s in words]
hi = print("This sentence can be recreated from positions", pos)
print(hi)saveFile = open("exampleFile.txt" , "w")
saveFile.write(hi)
saveFile.close()
However i get the error - TypeError: write() argument must be str, not None
and im not sure how to fix it
write('+'.join([str(x) for x in pos])) should work for you.
Replace the +
with whatever delimiter you want.
Similar to your original code line [words.index(s)+1 for s in words]
this list comprehension is a short form of a loop.
It takes every element in pos, names it x and applies the function str(x). The result of str(x) is then added to a new list.
So [1234]
is converted to a new list ['1','2','3','4']
.
Finally '+'.join(new list) joins all elements using '+'
as delimiter.
So we end up with the string 1+2+3+4
.
Note how this seems the same as above, but now it's characters, not numbers anymore.
The string is than the final parameter so python 'sees' write('1+2+3+4').