Предизвикателства > Разбий ни ключалката > Решения > Решението на Владимир Коцев

Резултати
0 точки от тестове
0 точки от учител

0 точки общо

0 успешни теста
1 неуспешни теста
Код

 1class LockPicker_6MI0600278:
 2    """Represents a lock picker and points checker."""
 3
 4    def __init__(self, lock):
 5        self.lock = lock
 6
 7    def unlock(self):
 8        try:
 9            self.lock.pick()
10        except TypeError as ex:
11            # In expected we know number of elements wanted
12
13            args = [ None ] * ex.expected # Creates a list with the right number of arguments
14
15        while True:
16            try:
17                self.lock.pick(args)
18                break # No errors
19            except TypeError as ex: # Wrong argument type
20                args[ex.position] = ex.expected() # The type is callable and gives default type value
21            except ValueError as ex: # Wrong value
22                args[ex.position] = ex.expected # Sets the right value to a position

Грешка при опит за отключване:
Traceback (most recent call last):
File "/tmp/solution.py", line 17, in unlock
self.lock.pick(args)
File "/tmp/test.py", line 50, in pick
raise LockTypeError("This is a different kind of lock.",
test.LockTypeError: This is a different kind of lock.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/tmp/test.py", line 98, in <module>
test_unlocker(LockPicker(lock))
File "/usr/local/lib/python3.12/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 77, in test_unlocker
unlocker.unlock()
File "/tmp/solution.py", line 20, in unlock
args[ex.position] = ex.expected() # The type is callable and gives default type value
^^^^^^^^^^^^^
TypeError: 'int' object is not callable

F
======================================================================
FAIL: test_picking (test.TestLockPicker.test_picking)
Ensure the lock is picked based on already stored boolean var.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 72, in test_picking
self.assertTrue(correct, "Не успя да отключиш.")
AssertionError: False is not true : Не успя да отключиш.

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

Дискусия
История

f1class LockPicker_6MI0600278:f1class LockPicker_6MI0600278:
2    """Represents a lock picker and points checker."""2    """Represents a lock picker and points checker."""
33
4    def __init__(self, lock):4    def __init__(self, lock):
5        self.lock = lock5        self.lock = lock
66
7    def unlock(self):7    def unlock(self):
8        try:8        try:
9            self.lock.pick()9            self.lock.pick()
10        except TypeError as ex:10        except TypeError as ex:
11            # In expected we know number of elements wanted11            # In expected we know number of elements wanted
1212
13            args = [ None ] * ex.expected # Creates a list with the right number of arguments13            args = [ None ] * ex.expected # Creates a list with the right number of arguments
1414
15        while True:15        while True:
16            try:16            try:
17                self.lock.pick(args)17                self.lock.pick(args)
18                break # No errors18                break # No errors
19            except TypeError as ex: # Wrong argument type19            except TypeError as ex: # Wrong argument type
t20                args[ex.position] = ex.expected() # The type is callable and givest20                args[ex.position] = ex.expected() # The type is callable and gives default type value
21            except ValueError as ex:21            except ValueError as ex: # Wrong value
22                args[ex.position] = ex.expected22                args[ex.position] = ex.expected # Sets the right value to a position
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op