1 回答
TA贡献1788条经验 获得超4个赞
该错误是因为一旦您读取文件,指针将在末尾,所以这就是发生该错误的原因,并且在调试后抛出了另一个错误,即dict对象没有append方法,我只是调试了它
import json
def read(accList):
with open('acc.json') as accountFile:
first = accountFile.read(1)
# This will make the pointer to point at the beggining of the file
accountFile.seek(0)
if not first:
print("No accounts")
else:
# this is to access only the account list and not the whole dictionary
accList["account"]=json.load(accountFile)["account"]
accountFile.close()
return(accList)
def write(accList):
with open('acc.json',"w") as accountFile:
accountFile.write(json.dumps(accList, indent = 4, sort_keys=True))
accountFile.close()
def login(accList):
accList=read(accList)
user=input("Please enter your username: ")
password=input("Please enter your password: ")
def create(accList):
accList=read(accList)
name=input("Please enter your name: ")
age =input("How old are you: ")
city = input("What city do you live in: ")
user=input("Please create a username: ")
password=input("Please create a password: ")
accList["account"].append({
"name": name,
"age": age,
"city": city,
"username": user,
"password": password
})
write(accList)
def main():
accList = {}
accList["account"] = []
returning=input("Welcome to Cael's login screen.\nDo you already have an account? ")
if(returning.lower()=="yes"):
login(accList)
elif(returning.lower()=="no"):
create(accList)
#else:
# print("Sorry that's not a valid answer. Please only type \'Yes\' or \'No\'.")
main()
添加回答
举报