Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # HST Playing - write a score and a name to an external file
- # then present the current 5 highest scores.
- # what we know
- a_list = [[15, "Gerald"],[20, "Stefano"],[5, "Marmaduke"]] # this is a list of lists similar to an array
- print(len(a_list)) ## your go
- print(a_list) # [[15, 'Gerald'], [20, 'Stefano'], [5, 'Marmaduke']]
- a_list.sort() # sorts increasing
- print(a_list) #[[5, 'Marmaduke'], [15, 'Gerald'], [20, 'Stefano']]
- a_list.sort(reverse = True) # sorts decreasing
- print(a_list) # [[20, 'Stefano'], [15, 'Gerald'], [5, 'Marmaduke']]
- # so we can use this for our HST (there are other ways to do this, but this one I undertsand)
- # Our Dice game can produce the score and name of the player and write it to a file
- # file called "HSTScores.txt"
- f = open("HSTScores.txt", "r") # r for read
- print(f.read()) # reads in and prints the whole file
- f.close() # always close
- """
- [15, 'Gerald']
- [20, 'Stefano']
- [5, 'Marmaduke']
- """
- # lets add a new score, someone played the game.. - assigned this to a list
- new_item = [500,'Freya the cat']
- f = open("HSTScores.txt","a") # a for append
- f.write(str(new_item)+"\n") # writing can only be done with string hence casting using str
- f.close()
- # reloading the file we now have added colins score
- """
- [15, 'Gerald']
- [20, 'Stefano']
- [5, 'Marmaduke]
- [100, 'Colin']
- """
- # all the data in the file is just a string, so we have some tinkering to do, line by line
- """
- hst_list = [] # to build our sortable list
- f = open("HSTScores.txt", "r") # read only
- for eachline in f: # to loop
- line = eachline
- hst_list.append(line)
- print(eachline, end = "")
- f.close()
- """
- # theres a problem, look at hst_list, it's lots of strings..
- """
- ["[15, 'Gerald']\n", "[20, 'Stefano']\n", "[5, 'Marmaduke]\n", "[100, 'Colin']\n"]
- """
- # whats needed, find the position of the comma, then string slice the score, and name
- # eg "[100, 'Colin']\n" needs to be 100 and "Colin"
- """
- f = open("HSTScores.txt", "r") # read only
- for eachline in f: # to loop this took me a while to see, hence all the print statement
- line = eachline
- print(line)
- comma_indx = line.find(",")
- print(comma_indx)
- score = line[1:comma_indx]
- print(score)
- scored_by = line[comma_indx+3:-3]
- print(scored_by)
- hst_list.append(line)
- print(eachline, end = "")
- f.close()
- """
- hst_list = [] # to build our sortable list
- # above can be reduced to
- f = open("HSTScores.txt", "r") # read only
- for eachline in f: # to loop this took me a while to see, hence all the print statement
- line = eachline
- comma_indx = line.find(",")
- score = int(line[1:comma_indx])
- scored_by = line[comma_indx+3:-3]
- new_item = [score,scored_by]
- hst_list.append(new_item)
- f.close()
- # yeah that was hard, there's probabaly an easier way, but we can explain this.. Honest
- # now the HST can be sorted and printed
- hst_list.sort(reverse = True)
- print("HIGH SCORES")
- if len(hst_list) < 5:
- for i in range(len(hst_list)):
- print(hst_list[i][0], hst_list[i][1])
- else:
- for i in range(5):
- print(hst_list[i][0], hst_list[i][1])
Advertisement
Advertisement