Romeme Romeme - 7 days ago 6
Python Question

Outputting on a file

This is my code

sentext = open("urSentence.txt", "w")
UserSen = input("Enter your sentence of your choice, ")
print (UserSen)
sentext.close()
postext = open("ThePos.txt", "w")
listSplit = UserSen.split()
X = {} #this will make the words in a sentence assigned to a number
position=[]
for i,j in enumerate(listSplit): #"i" will count how many words there are in the sentence
if j in X:
position.append(X[j])
else:
X[j]=i
position.append(i)
print (position)
postext.close()


It makes the files but it doesn't save anything in them. What am I doing wrong?

Answer

You never wrote to any file in any way. You can do that in a few ways. Since you're already using what looks like Python 3's print function, try the file parameter:

print(UserSen, file=sentext)

...

print(position, file=postext)