-1
def load():
    with open("random_number_highscores.txt","r") as x:
        print ("HIGHSCORES")
        print ("Least guesses made.")
        print (json.load(x))
        time.sleep(1)

def save(a):
    with open("random_number_highscores.txt", "a") as x:
        json.dump(a, x)
    print ("saved.")
    time.sleep(1)

为什么 def load 不起作用。我试过用 json.dump(str(a), x) 保存,但它也不起作用,只是得到错误

4

1 回答 1

1

您正在附加到保存文件,而您需要覆盖:

def save(a):
    with open("random_number_highscores.txt", "w") as x:
        json.dump(a, x)

否则代码会遇到多个 JSON 值,并且无法处理文件中的json.load()多个 JSON 值。

于 2013-09-01T23:04:28.147 回答