Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player:
- def __init__(self, name, score): # score < 100`000; name include only upper letters
- self.name = name
- self.score = score
- def file_reader(file_name_in, list_in):
- file = open(file_name_in, 'r')
- for i in range(10):
- name_f = file.readline().strip().upper()
- score_f = int(file.readline().strip())
- player = Player(name_f, score_f)
- list_in.append(player)
- def validation(name_in, score_in):
- if len(name_in) != 3:
- print("inappropriate name, please type another name")
- return False
- else:
- try:
- if score_in > 100000 or score_in < 1:
- print("too high or too low, it must be lower than 100`000 and higher than 0")
- return False
- else:
- return True
- except ValueError:
- print("this is not a number, try again >> ")
- return False
- def new_list_generator(p_in, old_list):
- flag = False
- res_list = []
- for p_old in old_list:
- if p_old.score > p_in.score or flag is True:
- res_list.append(p_old)
- else:
- res_list.append(p_in)
- flag = True
- res_list.append(p_old)
- if len(res_list) > 10:
- res_list.pop()
- return res_list
- def print_list(list_in):
- for player in list_in:
- print(player.name, " ", player.score)
- # Reading data from file to list
- top_ten_list = []
- file_name = "data.txt"
- file_reader(file_name, top_ten_list)
- # Creating a new player and validating input from used
- validation_succeed = False
- name_new = ""
- score_new = 0
- while not validation_succeed:
- name_new = input("please enter the name in 3 letters >> ").upper()
- score_new = int(input("please type the score, must be lower than 100`000 >> "))
- if validation(name_new, score_new):
- validation_succeed = True
- # Creating a new top ten list adding a new player
- p = Player(name_new, score_new)
- new_top_ten_list = new_list_generator(p, top_ten_list)
- # Printing new top ten list
- print_list(new_top_ten_list)
- # File data:
- # FYI
- # 10000
- # ABC
- # 9092
- # KEL
- # 8500
- # PAI
- # 8203
- # BBB
- # 7980
- # ACE
- # 7246
- # GKL
- # 7001
- # JSI
- # 6490
- # EIF
- # 6003
- # DIS
- # 2000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement