1 回答

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
該錯(cuò)誤是因?yàn)橐坏┠x取文件,指針將在末尾,所以這就是發(fā)生該錯(cuò)誤的原因,并且在調(diào)試后拋出了另一個(gè)錯(cuò)誤,即dict對(duì)象沒有append方法,我只是調(diào)試了它
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()
添加回答
舉報(bào)