Домашни > Функцията, която казва "Ni!" > Решения > Решението на Пресиан Андрейчев

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код
Скрий всички коментари

 1"""This is homework task 2 from Espresko."""
 2
 3
 4def function_that_says_ni(*args, **kwargs):
 5    """Check if a bush is a beauty and calculate its cost.If not returns Ni."""
 6    my_bushes = []
 7    total_cost_of_bushes = 0
 8
 9    for arg in args:
10        if isinstance(arg, dict) and "name" in arg:
11            name = arg["name"].lower()
12            if name in ["храст", "shrub", "bush"]:
13                cost = arg["cost"] if "cost" in arg else 0  # mmm so Ugly
14                if type(cost) in [int, float]:
15                    my_bushes.append(cost)
16
17    symbols_bushes = set()
18    for key, value in kwargs.items():
19        if hasattr(value, 'get') and "name" in value:
20            name = value["name"].lower()
21            if name in ["храст", "bush", "shrub"]:
22                cost = value["cost"] if "cost" in value else 0
23                if type(cost) in [int, float]:
24                    my_bushes.append(cost)
25                    symbols_bushes.update(key)
26
27    for ind in my_bushes:
28        total_cost_of_bushes += ind
29
30    if total_cost_of_bushes > 42.00:
31        return "Ni!"
32
33    int_number = int(total_cost_of_bushes)
34
35    if total_cost_of_bushes == 0:
36        return "Ni!"
37
38    result = len(symbols_bushes) % int_number
39    if int_number > 0 and result != 0:
40        return "Ni!"
41
42    returned_string = f"{total_cost_of_bushes:.2f}лв"
43    return returned_string

..E.......
======================================================================
ERROR: test_cost_whole_part_zero (test.TestNi.test_cost_whole_part_zero)
Test with a total cost part equal to zero.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 43, in test_cost_whole_part_zero
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 38, in function_that_says_ni
result = len(symbols_bushes) % int_number
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

----------------------------------------------------------------------
Ran 10 tests in 0.001s

FAILED (errors=1)

Дискусия
История
Това решение има само една версия.