繁体   English   中英

保存用户输入以便它们再次出现 - python 3.2

[英]Saving user inputs so that they appear again - python 3.2

我试图让我的用户输入保存但是,当我尝试这样做时,此消息出现在python shell中:

 nf.write('\n'.join(tempword))
io.UnsupportedOperation: not writable

这是我的部分代码:

 clues_list=[]
    userInput=input("Enter a symbol you would like to replace:")
    userInput2=input("What letter would you like to replace it with:")

    for word in words_list:
        tempword = (word)
        tempword = tempword.replace('#','A')
        tempword = tempword.replace('*', 'M')
        tempword = tempword.replace('%', 'N')
        tempword=tempword.replace(userInput,userInput2)
        print(tempword)
        clues_list.append(tempword)
        with open('words.txt', 'r') as nf:# bit that isnt working 
            nf.write('\n'.join(tempword))

基本上,我希望显示用户输入但不会发生。 有人可以向我解释为什么以及我需要做些什么来解决它? 问候

看起来你打开words.txt为只读,然后尝试写入它。 尝试改为:

with open('words.txt', 'w') as nf:
    nf.write('\n'.join(tempword))

请注意,在写入文件之前,这将使文件空白。 如果您需要附加到文件的末尾,请使用'a''append' )模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM