在fi上输出

2024-10-02 02:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

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()

它生成文件,但不保存任何内容。我做错什么了?你知道吗


Tags: theintxtcloseyourpositionopenwill
2条回答

你从未以任何方式写过任何文件。你可以用几种方法来做。既然您已经在使用类似python3的print函数,请尝试file参数:

print(UserSen, file=sentext)

...

print(position, file=postext)

print函数不会写入文件。您需要显式地写入它。你知道吗

sentext = open("urSentence.txt", "w")
UserSen = input("Enter your sentence of your choice, ")
sentext.write(UserSen)
sentext.close()

同样地:

postext = open("ThePos.txt", "w")
...
postext.write(str(position))
postext.close()

相关问题 更多 >

    热门问题