Advertisement
timber101

NoelsHST

Nov 7th, 2021
861
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None
  1. #  Noels Music game HST element
  2. #  steps
  3. #  1.  Save current score to our HST.txt file
  4. #  2.  Read in the file line by line into a list
  5. #  2a.  As the file is appended, split on the comma, convert score to int
  6. #  2b.  Close the file
  7. #  3.  Sort the list reverse order
  8. #  4.  Present the top 5 scores in decending order
  9.  
  10. """
  11. ##  Dummy entries to prove it works
  12. users_score = 170
  13. users_name = "Mercedes"
  14.  
  15. #  task 1  solution
  16. f = open("hst.txt", "a") # just opens the file and references it f
  17. f.write(str(users_score) + "," + users_name + "\n")
  18. f.close()  # have a look in the file
  19.  
  20. #  task 2  solution
  21. temp_hst_list = [] # an empty list to pump my .txt file into one line at a time
  22. f = open("hst.txt", "r") # open the file and reference it f
  23. fline = f.readline() # a variable to read each line into
  24. # print(fline) # prints one line at a time 23,roland
  25.  
  26. while fline != "":  # runs until nothing is read in
  27.    field = fline.split(",")  
  28.    score = int(field[0])
  29.    name = field[1]
  30.    
  31.    ##  print(field, score, name)  ['23', 'Roland\n'] 23 Roland
  32.    name = name[0:-1] #  gets rid of the \n
  33.    new_hst_list_item = [score, name]
  34.    temp_hst_list.append(new_hst_list_item)
  35.    
  36.    fline = f.readline()  # read in the next item
  37.  
  38. f.close()
  39. # print(temp_hst_list)  # now gives us a list of our score, with and int and str
  40.  
  41. # task 3  solution
  42. # print(temp_hst_list)
  43. temp_hst_list.sort(reverse = True)
  44. # print(temp_hst_list)  # results look a little scruffy?
  45.  
  46. # task 4  solution
  47. print("    *** HIGH SCORE TABLE ***")
  48. for i in range(5):
  49.    print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
  50. """
  51.  
  52.  
  53. #################################################
  54. # getting rid of all the guff from above we have
  55. # two blocks, a data entry and a HST Output
  56. #################################################
  57.  
  58. name_inpt = ""
  59. while name_inpt.upper() !="EXIT":
  60.     score_inpt = int(input("Enter Score >>>  "))
  61.     name_inpt = input("Enter Name or EXIT to Quit >>>  ")
  62.     f = open("hst.txt", "a")
  63.     f.write(str(score_inpt) + "," + name_inpt + "\n")
  64. f.close()
  65.  
  66. # HST output
  67. temp_hst_list = []
  68. f = open("hst.txt", "r")
  69. fline = f.readline()
  70.  
  71. while fline != "":  
  72.     field = fline.split(",")  
  73.     score = int(field[0])
  74.     name = field[1]
  75.     name = name[0:-1]
  76.     new_hst_list_item = [score, name]
  77.     if name.upper() !="EXIT":
  78.         temp_hst_list.append(new_hst_list_item)
  79.     fline = f.readline()
  80. f.close()
  81.  
  82. temp_hst_list.sort(reverse = True)
  83.  
  84. print("    *** HIGH SCORE TABLE ***")
  85. for i in range(5):
  86.     print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
  87.    
  88.  
  89.  
  90.  
  91.  
Advertisement
RAW Paste Data Copied
Advertisement