Создание файла и его обновление при необходимости - PullRequest
0 голосов
/ 29 апреля 2020

Я пытаюсь создать файл рекордов, затем ie и обновить его, если новый рекорд выше предыдущего. Тем не менее, несмотря на устранение ошибки за ошибкой, новая ошибка просто появляется каждый раз. В настоящее время, когда я запускаю, я получаю ошибку «Int объект не повторяется» в top_scores + = int (i). однако каждый элемент должен быть целым числом, поэтому я не знаю, что делать.

#this program would keep track of the player's score as he plays
#If the player reaches a good score it would be added to the score board
#It then saves the scores in a file to be later recalled should the player want to see the score
#This does not use names so it would only display the score in order from 1st to last
#There are a couple of issues I continue running into that I mention below



#This checks if file is already created if so it continues on the file if not it creates the file
try:
    file = open("highscores.txt")

    top_scoresR = file.readline()
    print(top_scoresR)
    if len(top_scoresR) < 3:
        for x in range(3 - len(top_scoresR)):
            top_scores[x] = 0
            print(top_scoresR)
    top_scores = []
    for i in top_scoresR:
        top_scores += int(i) 

    #for scores in range(3):
        #top_score[scores] = int(top_scores[scores])
    #top_scores[0] = int()
    #top_scores[1] = int
    #top_scores[2]

    #Im not sure how to recall the list from the file where it would also become integers you will see the problem when you run it
    #"ValueError: invalid literal for int() with base 10: '[500, 0, 0]'

    userinput = int(input("Whats your new score"))##This bit here would be the updated game scoresafter computer v player or player v player goes
    new_score = userinput
    file.close()
except IOError:
    file = open("highscores.txt", "a+")
    top_scores = [0,0,0]
    for x in range(3):
        file.write(str(top_scoresR[x]) + "\n")
    #file.write(str(top_scores[0]) + "\n")
    #file.write(str(top_scores[1])+ "\n")
    #file.write(str(top_scores[2])+ "\n")
    file.close()
    userinput = int(input("Whats your new score"))
    new_score = userinput
finally:
    file.close()

#This is the scoring system if updates the highscores if there is a new score worth adding
if new_score > top_scores[0]:
    top_scores[0] = new_score
elif new_score > top_scores[2] and new_score > top_scores[1] and new_score < top_scores[0]:
    top_scores[1] = top_scores[2]
    top_scores[1] = new_score
elif new_score > top_scores[2] and new_score < top_scores[1] and new_score < top_scores[0]:
    top_scores[2] = new_score

#This would be were the turtle code would be to write it out in turtle I put in basic because I don't know how much would change
print("High scores are: ")
print("1st ", top_scores[0])
print("2nd ", top_scores[1])
print("3rd ", top_scores[2])
file = open("highscores.txt", "w")#This rewrites the list with the new code
file.write(str(top_scores[0]) + "\n")
file.write(str(top_scores[1])+ "\n")
file.write(str(top_scores[2])+ "\n")
file.close()





...