简体   繁体   English

TypeError:读取文件时无法调用“ str”对象

[英]TypeError: 'str' object is not callable when reading a file

I am trying to make a simple game in python, (I'm pretty new, don't hate me too much!) and I'm getting this error when I run this in the idle editor: 我试图用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

this is my code: 这是我的代码:

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()

What am I doing wrong? 我究竟做错了什么?

read_file is now a string containing the content of the file. read_file现在是一个包含文件内容的字符串。 What you're doing is basically typing 'happy'() . 您要做的基本上是键入'happy'() Remove the '()' in read_file() . 删除read_file()的'()'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM