Advertisement
timber101

Dice Game 6 - High Score Table

Nov 13th, 2022 (edited)
743
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. #  HST Playing - write a score and a name to an external file
  2. #  then present the current 5 highest scores.
  3.  
  4. # what we know
  5.  
  6. a_list = [[15, "Gerald"],[20, "Stefano"],[5, "Marmaduke"]] # this is a list of lists similar to an array
  7.  
  8. print(len(a_list)) ## your go
  9.  
  10. print(a_list)                       # [[15, 'Gerald'], [20, 'Stefano'], [5, 'Marmaduke']]
  11. a_list.sort()                       # sorts increasing
  12. print(a_list)                       #[[5, 'Marmaduke'], [15, 'Gerald'], [20, 'Stefano']]
  13. a_list.sort(reverse = True)         # sorts decreasing
  14. print(a_list)                       # [[20, 'Stefano'], [15, 'Gerald'], [5, 'Marmaduke']]
  15.  
  16. # so we can use this for our HST (there are other ways to do this, but this one I undertsand)
  17.  
  18. # Our Dice game can produce the score and name of the player and write it to a file
  19. # file called "HSTScores.txt"
  20.  
  21. f = open("HSTScores.txt", "r")      # r for read
  22. print(f.read())                     # reads in and prints the whole file
  23. f.close()                           # always close
  24.  
  25. """
  26. [15, 'Gerald']
  27. [20, 'Stefano']
  28. [5, 'Marmaduke']
  29. """
  30.  
  31. # lets add a new score, someone played the game.. - assigned this to a list
  32. new_item = [500,'Freya the cat']
  33. f = open("HSTScores.txt","a")       # a for append
  34. f.write(str(new_item)+"\n")         # writing can only be done with string hence casting using str
  35. f.close()
  36.  
  37. # reloading the file we now have added colins score
  38. """
  39. [15, 'Gerald']
  40. [20, 'Stefano']
  41. [5, 'Marmaduke]
  42. [100, 'Colin']
  43. """
  44.  
  45. # all the data in the file is just a string, so we have some tinkering to do, line by line
  46. """
  47. hst_list = []       # to build our sortable list
  48.  
  49. f = open("HSTScores.txt", "r")      # read only
  50. for eachline in f:                  # to loop
  51.    line = eachline
  52.    hst_list.append(line)
  53.    print(eachline, end = "")
  54. f.close()
  55. """
  56.  
  57. # theres a problem, look at hst_list, it's lots of strings..
  58.  
  59. """
  60. ["[15, 'Gerald']\n", "[20, 'Stefano']\n", "[5, 'Marmaduke]\n", "[100, 'Colin']\n"]
  61. """
  62. #  whats needed, find the position of the comma, then string slice the score, and name
  63. #  eg "[100, 'Colin']\n"  needs to be 100 and "Colin"
  64.  
  65. """
  66. f = open("HSTScores.txt", "r")      # read only
  67. for eachline in f:                  # to loop  this took me a while to see, hence all the print statement
  68.    line = eachline
  69.    print(line)
  70.    comma_indx = line.find(",")
  71.    print(comma_indx)
  72.    score = line[1:comma_indx]
  73.    print(score)
  74.    scored_by = line[comma_indx+3:-3]
  75.    print(scored_by)
  76.    hst_list.append(line)
  77.    print(eachline, end = "")
  78. f.close()
  79. """
  80. hst_list = []       # to build our sortable list
  81.  
  82. # above can be reduced to
  83. f = open("HSTScores.txt", "r")      # read only
  84. for eachline in f:                  # to loop  this took me a while to see, hence all the print statement
  85.     line = eachline
  86.     comma_indx = line.find(",")
  87.     score = int(line[1:comma_indx])
  88.     scored_by = line[comma_indx+3:-3]
  89.     new_item = [score,scored_by]
  90.     hst_list.append(new_item)
  91. f.close()
  92.  
  93. # yeah that was hard, there's probabaly an easier way, but we can explain this..  Honest
  94.  
  95. # now the HST can be sorted and printed
  96.  
  97. hst_list.sort(reverse = True)
  98. print("HIGH SCORES")
  99. if len(hst_list) < 5:
  100.     for i in range(len(hst_list)):
  101.         print(hst_list[i][0], hst_list[i][1])
  102.    
  103. else:
  104.     for i in range(5):
  105.         print(hst_list[i][0], hst_list[i][1])
  106.    
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement