[英]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.