0

My coding here:

option_input = raw_input(">>>")
option_input = option_input.upper()
if option_input == "V":
    print "High Scores:"
    saveFile = open("scores.txt", 'r')
    scores = saveFile.read()
    saveFile.close()
    nameScore= []
    for line in scores.split('\n'):
        # skip any empty line
        if not line:
            continue
        userScore, score = line.split(',')
        #convert score to a numeric value
        score = int(score)
        nameScore.append((score, userScore))
    print "%-18s %s" % (nameScore, score)

the result still show up :

High Scores:
[(13, 'Matthew'), (6, 'Luke'), (3, 'John'), (3, 'john')] 3

I want to show this up like

13, Matthew
 6, Luke
 3, john

all should be align..
I really looking forward your help :)

Edited by vegaseat: added code tags

4
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by pyTony
Featured Replies
  • There are different ways to do it. Here is one [code=python] >>> for score, name in nameScore: ... print "{0}, {1}".format(score, name) ... 13, Matthew 6, Luke 3, John 3, john [/code] Also, use code tags when you post python code. See here [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3[/url] Read More

0

Maybe something like this:

option_input = raw_input(">>> ").upper()
if option_input == "V":
    print "High Scores:\n"
    saveFile = open("scores.txt", "r")
    scores = saveFile.readlines()
    for line in scores:
        if "," in line:
            userScore, userName = line.split(',')
            print "%2s, %s" % (userScore, userName.strip())

At the end of the program you're assigning the variables wrong and end up creating tuples. You need to have your print statement in the loop to cycle through the lines.

0

What is wrong with:

print(open("scores.txt").read())

Just save the scores correctly in first place.

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.