Advertisement
IlyaIlinich

Top ten list from file

Jun 20th, 2024
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1.  class Player:
  2.     def __init__(self, name, score):  # score < 100`000; name include only upper letters
  3.         self.name = name
  4.         self.score = score
  5.  
  6.  
  7. def file_reader(file_name_in, list_in):
  8.     file = open(file_name_in, 'r')
  9.     for i in range(10):
  10.         name_f = file.readline().strip().upper()
  11.         score_f = int(file.readline().strip())
  12.         player = Player(name_f, score_f)
  13.         list_in.append(player)
  14.  
  15.  
  16. def validation(name_in, score_in):
  17.     if len(name_in) != 3:
  18.         print("inappropriate name, please type another name")
  19.         return False
  20.     else:
  21.         try:
  22.             if score_in > 100000 or score_in < 1:
  23.                 print("too high or too low, it must be lower than 100`000 and higher than 0")
  24.                 return False
  25.             else:
  26.                 return True
  27.         except ValueError:
  28.             print("this is not a number, try again >> ")
  29.             return False
  30.  
  31.  
  32. def new_list_generator(p_in, old_list):
  33.     flag = False
  34.     res_list = []
  35.     for p_old in old_list:
  36.         if p_old.score > p_in.score or flag is True:
  37.             res_list.append(p_old)
  38.         else:
  39.             res_list.append(p_in)
  40.             flag = True
  41.             res_list.append(p_old)
  42.     if len(res_list) > 10:
  43.         res_list.pop()
  44.     return res_list
  45.  
  46.  
  47. def print_list(list_in):
  48.     for player in list_in:
  49.         print(player.name, "        ", player.score)
  50.  
  51.  
  52. # Reading data from file to list
  53. top_ten_list = []
  54. file_name = "data.txt"
  55. file_reader(file_name, top_ten_list)
  56.  
  57. # Creating a new player and validating input from used
  58. validation_succeed = False
  59. name_new = ""
  60. score_new = 0
  61. while not validation_succeed:
  62.     name_new = input("please enter the name in 3 letters >> ").upper()
  63.     score_new = int(input("please type the score, must be lower than 100`000 >> "))
  64.     if validation(name_new, score_new):
  65.         validation_succeed = True
  66.  
  67. # Creating a new top ten list adding a new player
  68. p = Player(name_new, score_new)
  69. new_top_ten_list = new_list_generator(p, top_ten_list)
  70.  
  71. # Printing new top ten list
  72. print_list(new_top_ten_list)
  73.  
  74. # File data:
  75. # FYI
  76. # 10000
  77. # ABC
  78. # 9092
  79. # KEL
  80. # 8500
  81. # PAI
  82. # 8203
  83. # BBB
  84. # 7980
  85. # ACE
  86. # 7246
  87. # GKL
  88. # 7001
  89. # JSI
  90. # 6490
  91. # EIF
  92. # 6003
  93. # DIS
  94. # 2000
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement