def scoreScreen(): #Keeps Calling
    score = 999999/canvas.data.timer
    canvas.create_rectangle(0,0,1000,460, fill = "black")
    canvas.create_text(500,200, text = "Your Score was", font =
                       ("Comic Sans MS Bold", 20, "bold"), fill =  "Red")
    canvas.create_text(670,200, text = str(score), font =
                       ("Comic Sans MS Bold", 20, "bold"), fill =  "Red")
    canvas.create_text(500,400, text = "You have beat the gam.e Enter your name, and then you may exit the game!)", font =
                       ("Comic Sans MS Bold", 12, "bold"), fill =  "Red")
    user = raw_input("Enter your name!")
    listOfOldScores = loadTextList("high scores.txt")
    listOfOldScores.append((user, score))
    saveText(str(listOfOldScores), "high scores.txt")


# Loads a text file into a single string
# Parameters:
#   fileName: a string with the name of the file to be loaded
# Returns a string
def loadTextString(fileName):
    fileHandler = open(fileName, "rt") # rt stands for read text
    text = fileHandler.read() # read the entire file into a single string
    fileHandler.close() # close the file
    return text


# Loads a text file into a list of strings. Each string is one line of text.
# Parameters:
#   fileName: a string with the name of the file to be loaded
# Returns a list of strings
def loadTextList(fileName):
    fileHandler = open(fileName, "rt") # rt stands for read text
    text = fileHandler.readlines() # read the entire file into a list
    fileHandler.close() # close the file
    return text

# Saves a string into a file.
# If the file already exists, it will be overwritten.
# Parameters:
#   fileName: a string with the name of the file to be written
# Returns None
def saveText(text, fileName):
  fileHandler = open(fileName, "wt") # wt stands for write text
  fileHandler.write(text) # write the text
  fileHandler.close() # close the file

After this first name/score is saved, it gets nasty. After 3 it looks like:

", (\'Brown Smight\', 2053)]', ('Meow Meow', 2053)]

I need to it be in a list of tuples or something similar so I can g through them in another function to display on a high score screen with tkinter. Help please!

Recommended Answers

All 4 Replies

You read the file into a list but write the file as one string. Instead write one tuple per record. And delete loadTextString() as that is just confusing things.

save_text(listOfOldScores, "high scores.txt")
#
def save_text(scores_list, file_name):
    file_handler = open(file_name, "w")
    for tup in scores_list:
        file_handler.write("%s, %d\n" % (tup[0], tup[1]))  ## name and score
    file_handler.close()

Something isn't right. It was giving me a (digit expected, not a string) error before, so i modified the code a bit to get no error, but now it's not doing as I please. The first name goes fine, so I'm guessing the loading is off.


Result in File after inputting Charlie Sheen, Coco Puffs and Fruit Puffs

C, ,Fruit Puffs, 1394

def scoreScreen(): #Keeps Calling
    score = str(999999/canvas.data.timer)
    canvas.create_rectangle(0,0,1000,460, fill = "black")
    canvas.create_text(500,200, text = "Your Score was", font =
                       ("Comic Sans MS Bold", 20, "bold"), fill =  "Red")
    canvas.create_text(670,200, text = score, font = ("Comic Sans MS Bold", 20, "bold"), fill =  "Red")
    canvas.create_text(500,400, text = "You have beat the game Enter your name [/n] and then you may exit the game!)", font =
                       ("Comic Sans MS Bold", 12, "bold"), fill =  "Red")
    user = raw_input("Enter your name!")
    listOfOldScores = loadTextList("high scores.txt")
    listOfOldScores.append((user, score))

    
    save_text(listOfOldScores, "high scores.txt")


# Loads a text file into a list of strings. Each string is one line of text.
# Parameters:
#   fileName: a string with the name of the file to be loaded
# Returns a list of strings
def loadTextList(fileName):
    fileHandler = open(fileName, "rt") # rt stands for read text
    text = fileHandler.readlines() # read the entire file into a list
    fileHandler.close() # close the file
    return text


def save_text(scores_list, file_name):
    file_handler = open(file_name, "w")
    for tup in scores_list:
        file_handler.write("%s, %s" % (tup[0], tup[1])) ## name and score
    file_handler.close()

Figured it out myself. Thanks anyways.

Could you post the final code, I'm curious how you finished it.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.