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

  • Home
  • SEARCH
  • 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 7837607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:45:07+00:00 2026-06-02T14:45:07+00:00

I am very new to python, and I am hoping I didnt miss a

  • 0

I am very new to python, and I am hoping I didnt miss a fix for this somewhere else. I have a simple program that was one of the practice excercises in a book I purchased and I am running into an issue. I have a program that opens a file and writes it to a list. Then a user can update the list with input, and when a user exits it updates the list with the latest content. Everything works fine except the sort option. It shows the scores from the file with a single quote infront of them, and the scores updated while the program was running without. It also doesn’t sort them at all. I have tried many different way to do this without fail. I am sure this is not that important in the long run, but I wanted to figure it out.

Here is the code

# High Scores
# Demonstrates list methods

scores = []
try:
    text_file = open("scores.txt", "r")
    for line in text_file:
        scores.append(line.rstrip("\n"))

    text_file.close()

except:
    raw_input("Please verify that scores.txt is placed in the correct location and run again")



choice = None
while choice != "0":


    print \
    """
    High Scores Keeper

    0 - Exit
    1 - Show Scores 
    2 - Add a Score
    3 - Delete a Score 
    4 - Sort Scores
    """

    choice = raw_input("Choice: ")
    print

    # exit
    if choice == "0":
        try:
            output_file = open("scores.txt" , "w")
            for i in scores:
                output_file.write(str(i))
                output_file.write("\n")

            output_file.close()
            print "Good-bye"
        except:
            print "Good-bye.error"

    # list high-score table
    elif choice == "1":
        print "High Scores"
        for score in scores:
            print score

    # add a score
    elif choice == "2":
        score = int(raw_input("What score did you get?: "))
        scores.append(score)

    # delete a score
    elif choice == "3":
        score = int(raw_input("Delete which score?: "))
        if score in scores:
            scores.remove(score)
        else:
            print score, "isn't in the high scores list."

    # sort scores
    elif choice == "4":
        scores.sort()
        scores.reverse()
        print 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-02T14:45:08+00:00Added an answer on June 2, 2026 at 2:45 pm

    When you add scores from the file, you’re adding them as strings: scores.append(line.rstrip("\n")). But when you add scores during the program, you’re adding them as integers: int(raw_input("What score did you get?: ")).

    When Python sorts a list containing both strings and integers, it’ll sort the strings according to character order (so '1' < '12' < '3'), and sort the integers separately, putting the integers before the strings:

    >>> sorted([1, 8, '11', '3', '8'])
    [1, 8, '11', '3', '8']
    

    Presumably it’s printing out a single quote after the characters as well as before, as it does here (indicating that it’s a string).

    So, when you’re reading the file at the start, turn them into an integer just like you do when you read user input.


    Some other tips:

    • scores.sort(reverse=True) will sort in reverse order without having to go through the list twice.
    • It’s generally a bad idea to do except:: that’ll catch absolutely any problem with the program, including the user hitting ^C to try to quit, the system running out of memory, etc. You should do except Exception: as a catch-all to get exceptions that it’s possible to recover from but not those kinds of system errors, or a more specific exception when you want to handle only certain types.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to python and can't understand why this isn't working. I have a
I am very new to Python and I have a json news feed that
I am very new to python. I am trying to write a program that
I'm very new to Python and have a question. Currently I'm using this to
I am very new with Python and I have just received this message while
I'm very new to Python and do know that my question is very simple
I have a very very simple program that parses a csv file that has
I'm very new to Python and trying to write a program that parses some
I very new to Python, and fairly new to regex. (I have no Perl
I am very new to python programming and have yet to buy a textbook

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.