typeError:读取文件时“ str”对象是不可调的



我正在尝试在Python中制作一个简单的游戏,(我很新,不要恨我太多!),当我运行此操作时,我会遇到此错误空闲编辑器:

Do you have an account? Y or N: n
What is your username: password
Traceback (most recent call last):
  File "C:UsersBright BridgeDesktopPersonalPython Projectsmain.py", line 33, in <module>
    check_user_name()
  File "C:UsersBright BridgeDesktopPersonalPython Projectsmain.py", line 17, in check_user_name
    if set_user_name() in read_file().format():
TypeError: 'str' object is not callable

这是我的代码:

def create_file():
    security = open("user.txt", "r+") #I know it's called security, but it's
    security.write(set_user_name())   #not really secure...
    security.close()
def check_user_name():
    security = open("user.txt", "r")
    read_file = security.read()
    if set_user_name() in read_file().format():
        print("Welcome " + set_user_name())
        security.close()
    else:
        print("Invalid Username")
        security.close()
def login(username, password):
    pass
def set_user_name():
    user_name = input("What is your username: ")
    return user_name
account_check = input("Do you have an account? Y or N: ")
if account_check == "y" or "Y" or "yes" or "Yes" or "YES":
    check_user_name()
else:
    create_file()

我在做什么错?

read_file现在是包含文件内容的字符串。您正在做的基本上是键入'happy'()。删除read_file()中的"()"。

最新更新