Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Noels Music game HST element
- # steps
- # 1. Save current score to our HST.txt file
- # 2. Read in the file line by line into a list
- # 2a. As the file is appended, split on the comma, convert score to int
- # 2b. Close the file
- # 3. Sort the list reverse order
- # 4. Present the top 5 scores in decending order
- """
- ## Dummy entries to prove it works
- users_score = 170
- users_name = "Mercedes"
- # task 1 solution
- f = open("hst.txt", "a") # just opens the file and references it f
- f.write(str(users_score) + "," + users_name + "\n")
- f.close() # have a look in the file
- # task 2 solution
- temp_hst_list = [] # an empty list to pump my .txt file into one line at a time
- f = open("hst.txt", "r") # open the file and reference it f
- fline = f.readline() # a variable to read each line into
- # print(fline) # prints one line at a time 23,roland
- while fline != "": # runs until nothing is read in
- field = fline.split(",")
- score = int(field[0])
- name = field[1]
- ## print(field, score, name) ['23', 'Roland\n'] 23 Roland
- name = name[0:-1] # gets rid of the \n
- new_hst_list_item = [score, name]
- temp_hst_list.append(new_hst_list_item)
- fline = f.readline() # read in the next item
- f.close()
- # print(temp_hst_list) # now gives us a list of our score, with and int and str
- # task 3 solution
- # print(temp_hst_list)
- temp_hst_list.sort(reverse = True)
- # print(temp_hst_list) # results look a little scruffy?
- # task 4 solution
- print(" *** HIGH SCORE TABLE ***")
- for i in range(5):
- print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
- """
- #################################################
- # getting rid of all the guff from above we have
- # two blocks, a data entry and a HST Output
- #################################################
- name_inpt = ""
- while name_inpt.upper() !="EXIT":
- score_inpt = int(input("Enter Score >>> "))
- name_inpt = input("Enter Name or EXIT to Quit >>> ")
- f = open("hst.txt", "a")
- f.write(str(score_inpt) + "," + name_inpt + "\n")
- f.close()
- # HST output
- temp_hst_list = []
- f = open("hst.txt", "r")
- fline = f.readline()
- while fline != "":
- field = fline.split(",")
- score = int(field[0])
- name = field[1]
- name = name[0:-1]
- new_hst_list_item = [score, name]
- if name.upper() !="EXIT":
- temp_hst_list.append(new_hst_list_item)
- fline = f.readline()
- f.close()
- temp_hst_list.sort(reverse = True)
- print(" *** HIGH SCORE TABLE ***")
- for i in range(5):
- print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement