Python Programming for the Absolute Beginner - Chap 7 Q: 2

A forum for general discussion of the Python programming language.

Python Programming for the Absolute Beginner - Chap 7 Q: 2

Postby zacksolutions » Mon Aug 12, 2013 1:25 pm

Hello,

I am a python newbie. I am reading this book (Python Programming for the Absolute Beginner). I am on Chapter 7, Question 2.

"Improve the Trivia Challenge game so that it maintains a high-scores list in a file. The program should record the player's name and score. Store the high scores using a pickled object."
I have the following code so far:

Code: Select all
def high_score():
    """Records a player's score"""

    high_scores = []

    #add a score // Do current stuff for adding a new score...
    name = input("What is your name? ")
    player_score = int(input("What is your score? "))
    entry = (name, player_score)
    high_scores.append(entry)
    high_scores.sort(reverse=True)
    high_scores = high_scores[:5]       # keep only top five

    # dump scores
    f = open("pickles1.dat", "wb")
    pickle.dump(high_scores, f)
    f.close()

    f = open("pickles1.dat", "rb")
    high_scores = pickle.load(f)
    print(high_scores)
    f.close()


When I execute this program in the main() program I get only the existing single name, player_score list combination stored in the pickles1.dat file.

Can someone walk me through how it can store all the values each time the program is ran?

Thanks,
zacksolutions
Last edited by stranac on Mon Aug 12, 2013 1:42 pm, edited 1 time in total.
Reason: Added code tags
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby Kebap » Mon Aug 12, 2013 1:47 pm

Hi zacksolutions,

I am not sure, if I understand your question correctly. (If I don't, please help me help you, check my signature)

If you want to save more than one (=keep the old scores when you add a new one), you need to change your program logic a bit.

First, you need to open the "old" highscores from disk, not just create an empty list for them. Then add the new score. Then save the total combined.

I am sure this would work (in theory). Let us know how it works for you. :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby zacksolutions » Mon Aug 12, 2013 2:28 pm

Hi Kebap,

Looks like you put me on the right track. This code looks to have worked:

Code: Select all
def high_score():
    """Records a player's score"""

    try:
        with open("pickles1.dat", "rb") as f:
            high_scores = pickle.load(f)
    except EOFError:
        high_scores = []

    #add a score // Do current stuff for adding a new score...
    name = input("What is your name? ")
    player_score = int(input("What is your score? "))
    entry = (name, player_score)
    high_scores.append(entry)
    high_scores.sort(reverse=True)
    high_scores = high_scores[:5]       # keep only top five

    # dump scores
    f = open("pickles1.dat", "wb")
    pickle.dump(high_scores, f)
    f.close()

    f = open("pickles1.dat", "rb")
    high_scores = pickle.load(f)
    print(high_scores)
    f.close()
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby stranac » Mon Aug 12, 2013 2:41 pm

Read the PM I sent you, then read the link in there.
I'll be mad if you ignore me. :evil:
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby zacksolutions » Mon Aug 12, 2013 3:01 pm

Hi stranac,
I read your PM. Thanks for the heads up.

Going back to my program, I found a flaw. The code below sorts the nested elements of a list in the pickled object by key (or player's name) order. How can I sort the nested elements of the list in descending value order?

Code: Select all
high_scores.sort(reverse=True)


TIA,
zacksolutions
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby stranac » Mon Aug 12, 2013 5:01 pm

You'll need to provide a key function.

You can find some examples in the docs: http://wiki.python.org/moin/HowTo/Sorti ... _Functions
The second example is really close to what you need.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby zacksolutions » Tue Aug 13, 2013 1:32 am

Hello stranac,

Thanks for pointing me to the right direction. This line worked for me! It displays the top 5 high scores in descending score order.

Code: Select all
high_scores = sorted(high_scores[:5], key=lambda score: score[1], reverse=True)


Thanks again,
zacksolutions
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Python Programming for the Absolute Beginner - Chap 7 Q:

Postby micseydel » Tue Aug 13, 2013 2:45 am

What you did is logically flawed, it only works if the first five elements are the top 5 already. You need to do the slicing outside the sorting.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1131
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests