什么是"AttributeError:"_ io.TextIOWrapper"对象没有属性"replace"在python中? [英] What is a " AttributeError: '_io.TextIOWrapper' object has no attribute 'replace' " in python?
问题描述
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,"\n")
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
所以基本上我正在做一个计算机科学项目,我的任务是通过将符号替换为字母来制作解码游戏,但是当我尝试制作实际上将符号变成字母的代码部分时,却遇到了这个错误.
So basically I'm doing a computer science project and my task is to make a decoding game by substituting symbols to letters but I get this error for when I try to make the part of the code which actually changes symbols into letters.
还有什么方法可以使该循环(不使用while
循环,因为它们非常复杂)?我基本上希望代码在运行程序时向我显示A和B,并在选择任何选项后为我显示能够选择其他选项的代码. (例如,我按0进行说明,然后可以选择其他选项,例如开始游戏.)
Also is there any way to make this loop (not using while
loops as they are quite complicated)? I basically want the code to show me the A and B when I run the program and for me after choosing any option to be able to choose a different option. (Eg I press 0 for instructions and then be able to choose a different option such as start game).
推荐答案
这部分代码:
text_file = open ("words.txt","r")
contents = text_file
print (text_file.read())
text_file.close()
没有任何意义.您正在将文件对象(不是文件内容的 )分配给contents
.然后,您print
内容,但不要将其分配给任何内容.我想您想要的是:
makes no sense. You are assigning the file object (not the file's contents) to contents
. You then print
the contents, but don't assign them to anything. I think what you want is:
with open("words.txt") as text_file:
contents = text_file.read()
print(contents)
这篇关于什么是"AttributeError:"_ io.TextIOWrapper"对象没有属性"replace"在python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!