What is a “ AttributeError: '_io.TextIOWrapper' object has no attribute 'replace' ” in python?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | print ( """ Welcome to the code breaker game! In this game you will have to change symbols into letters in order to decipher secret words. 0 - instructions 1 - start 2 - clues 3 - check your answers 4 - quit """) choice = input(" choice :") if choice == ("0"): text_file = open ("instructions.txt","r") print (text_file.read()) text_file.close() elif choice =="1": text_file = open ("words.txt","r") contents = text_file print (text_file.read()) text_file.close() a = input("Please enter a symbol") b = input("Please enter a letter") newcontents = contents.replace(a,b) contents = newcontents print(contents," ") text_file.close elif choice =="2": text_file = open ("clues.txt","r") print (text_file.read()) text_file.close() elif choice =="3": text_file = open ("solved.txt","r") print (text_file.read()) text_file.close() elif choice =="4": quit |
所以基本上我正在做一个计算机科学项目,我的任务是通过将符号替换成字母来制作解码游戏,但是当我尝试将符号的一部分实际上改为字母时,我得到了这个错误。
也有任何方法可以进行此循环(不使用
这部分代码:
1 2 3 4 | text_file = open ("words.txt","r") contents = text_file print (text_file.read()) text_file.close() |
没有意义。 您正在将文件对象(而不是文件的内容)分配给
1 2 3 | with open("words.txt") as text_file: contents = text_file.read() print(contents) |