Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8883665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:52:13+00:00 2026-06-14T20:52:13+00:00

I would appreciate any help with my homework assignment – It’s a simple program

  • 0

I would appreciate any help with my homework assignment – It’s a simple program for class that is supposed to check for a file, if it exists, it reads the file, and loads the data into the program, so you can list scores, and add more of them. Its supposed to keep only the top 5 scores.

Then when you close the program (by choosing option 0) it should write the top 5 scores to the scores.txt file. I think I got that to work properly, I’m just having trouble getting the program to read and populate the scores file correctly.

Here’s my code so far:

scores = []

#Check to see if the file exists
try:
    file = open("scores.txt")
    for i in range(0, 5):
        name = file.readline()
        score = file.readline()
        entry = (score, name)
        scores.append(entry)
        scores.sort()
        scores.reverse()
        scores = scores[:5]
    file.close()
except IOError:
    print "Sorry could not open file, please check path."


choice = None
while choice != "0":

    print    """
    High Scores 2.0

    0 - Quit
    1 - List Scores
    2 - Add a Score
    """


    choice = raw_input("Choice: ")
    print ""

    # exit
    if choice == "0":
        print "Good-bye."
        file = open("scores.txt", "w+")
        #I kinda sorta get this now... kinda...
        for entry in scores:
            score, name = entry
            file.write(name)
            file.write('\n')
            file.write(str(score))
            file.write('\n')
        file.close()

    # display high-score table
    elif choice == "1":
        print "High Scores\n" 
        print "NAME\tSCORE" 
        for entry in scores:
            score, name = entry    
            print name, "\t", score

    # add a score
    elif choice == "2":
        name = raw_input("What is the player's name?: ")
        score = int(raw_input("What score did the player get?: "))
        entry = (score, name)
        scores.append(entry)
        scores.sort()
        scores.reverse()
        scores = scores[:5]     # keep only top 5 scores

    # some unknown choice
    else:
        print "Sorry, but", choice, "isn't a valid choice." 

raw_input("\n\nPress the enter key to exit.")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:52:15+00:00Added an answer on June 14, 2026 at 8:52 pm

    You should try writing your file in a Comma-Separated-Value (CSV). While the term uses the word “comma”, the format really just means any type of consistent field separator, with each record on one line.

    Python has a csv module to help with reading and writing this format. But I am going to ignore that and do it manually for your homework purposes.

    Let’s assume you have a file like this:

    Bob,100
    Jane,500
    Jerry,10
    Bill,5
    James,5000
    Sara,250
    

    I am using comma’s here.

    f = open("scores.txt", "r")
    scores = []
    for line in f:
        line = line.strip()
        if not line:
            continue
        name, score = line.strip().split(",")
        scores.append((name.strip(), int(score.strip())))
    
    print scores
    """
    [('Bob', 100),
     ('Jane', 500),
     ('Jerry', 10),
     ('Bill', 5),
     ('James', 5000),
     ('Sara', 250)]
    """
    

    You don’t have to sort the list every time you read and append. You can do it once at the end:

    scores.sort(reverse=True, key=lambda item: item[1])
    top5 = scores[:5]
    

    I realize lambda may be new to you. It is an anonymous function. We use it here to tell the sort function where to find the key for the comparisons. In this case we are saying for each item in the scores list, use the score field (index 1) for comparisons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would appreciate any help that can be provided with this matter. I am
I would appreciate any help on this issue. Lets say I want to load
I'm relatively new to Java and would appreciate any help on this! I have
I would really appreciate any help with the following problem: I need to be
Would very much appreciate any help or hint on were to go next. I'm
I would really appreciate any jquery expert help as a method I've used previously,
I'm very new to C++ and would really appreciate any and all help. I
Any help with this problem would be fantastic. I appreciate all contributions! Let us
So for my assignment, I am supposed to implement a Node class that just
Hello would appreciate any help having problems with this code. value=<?phpif($_GET['level']== Grade 2){ echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.