[Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Mar 18 13:37:14 EDT 2020
On 18/03/2020 12:33, Adam Ćuszcz wrote:
> def high_score():
>
> """Records a player's score"""
> high_scores = []
Note that this creates a new empty list.
>
> #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()
Despite the comments the above code only stores a single name....
It overwrites anything else that may have been in the file.
> f = open("pickles1.dat", "rb")
> high_scores = pickle.load(f)
> print(high_scores)
> f.close()
And this reads it back.
> 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.
What else did you expect to get?
Only one name is stored and therefore only one name is read.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list