1- from random import shuffle
2- from uuid import uuid1
3- from os import listdir
1+ def pointless_fun (dictionary , num ):
2+ for key in dictionary :
3+ if len (key ) == num :
4+ dictionary [key ] += '+'
5+ return dictionary
46
5- no_test_error_msg = 'Теста не существует или введен неправильный номер'
67
7-
8- def get_parsed_test (f ):
9- test = {}
10-
11- cur_task = []
12- for line in f :
13- line = line .strip ()
14-
15- if not line .isdigit ():
16- cur_task .append (line )
17- else :
18- test [line + '_' + uuid1 ().hex ] = cur_task
19- cur_task = []
20-
21- return test
22-
23-
24- def game (test_name ):
25- try :
26- f = open (f'tests/{ test_name } .txt' )
27- except FileNotFoundError :
28- exit (no_test_error_msg )
29- tasks = get_parsed_test (f )
30- test_ids = list (tasks .keys ())
31- shuffle (test_ids )
32-
33- score = 0
34- for test_id in test_ids :
35- task = tasks [test_id ]
36- question = task .copy ()[0 ]
37- task = task [1 ::]
38- right_answer = task .copy ()[int (test_id .split ('_' )[0 ])- 1 ]
39- shuffle (task )
40-
41- print (f'Вопрос: { question } ' )
42- for answer_index in range (0 , len (task )):
43- print (f'{ answer_index + 1 } -> { task [answer_index ]} ' )
44-
45- if right_answer == task [int (input ('Введите номер ответа: \n ' ).strip ())- 1 ]:
46- score += 1
47-
48- f .close ()
49- return f'{ score } /{ str (len (tasks ))} '
50-
51-
52- def update_leader_board (user_name , score , test_name ):
53- f = open ('leader_board.txt' , 'r' )
54- f_string = f .read ()
55- f .close ()
56- f_string += f'{ user_name } -> { score } -> { test_name } \n '
57- f = open ('leader_board.txt' , 'w+' )
58- f .write (f_string )
59- f .close ()
60- return f_string
61-
62-
63- def main ():
64- user_name = input ('Введите имя: ' )
65- tests = listdir ('tests/' )
66- for i in range (len (tests )):
67- tests [i ] = tests [i ].replace ('.txt' , '' )
68- for i in range (0 , len (tests )):
69- print (f'{ i + 1 } -> { tests [i ]} ' )
70-
71- try :
72- test_index = int (input ('Введите номер теста:\n ' )) - 1
73- if test_index < 0 :
74- raise IndexError
75- test_name = tests [test_index ]
76- except IndexError :
77- exit (no_test_error_msg )
78-
79- score = game (test_name )
80- print (update_leader_board (user_name , score , test_name ))
81-
82-
83- if __name__ == '__main__' :
84- main ()
8+ print (
9+ pointless_fun ({'a' : 'fas' , 'b' : '2ads' }, 1 )
10+ )
0 commit comments