简体   繁体   English

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

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

I am trying to get my user input to save however, when i have tried doing so, this message comes up in the python shell: 我试图让我的用户输入保存但是,当我尝试这样做时,此消息出现在python shell中:

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

Here is my code for that section: 这是我的部分代码:

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

Basically, i want the user input to be displayed however that isnt happening. 基本上,我希望显示用户输入但不会发生。 Can someone please explain to me why and what i need to do to fix it ? 有人可以向我解释为什么以及我需要做些什么来解决它? Regards 问候

Looks like you're opening words.txt as read only, then trying to write to it. 看起来你打开words.txt为只读,然后尝试写入它。 Try instead: 尝试改为:

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

Note that this will blank your file before writing to it. 请注意,在写入文件之前,这将使文件空白。 If you need to append to the end of your file, use the 'a' ( 'append' ) mode instead. 如果您需要附加到文件的末尾,请使用'a''append' )模式。

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

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