TypeError:读取文件时,'str'对象不可调用

时间:2017-03-16 02:04:16

标签: python file

我正在尝试在python中制作一个简单的游戏,(我很新,不要太讨厌我了!)当我在空闲编辑器中运行时,我收到了这个错误:

Do you have an account? Y or N: n
What is your username: password
Traceback (most recent call last):
  File "C:\Users\Bright Bridge\Desktop\Personal\Python Projects\main.py", line 33, in <module>
    check_user_name()
  File "C:\Users\Bright Bridge\Desktop\Personal\Python Projects\main.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()

我做错了什么?

1 个答案:

答案 0 :(得分:4)

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