Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
790 B
1
Indexable
Never
def factorial(n):
   
    if n < 0:
        raise ValueError
    elif n == 0:
        return 1
    else:
        return n * factorial(n - 1)

print(factorial(4))  
print(factorial(9)) 


def add_nested_elements(nested_list):
    total = 0
    for element in nested_list:
        if isinstance(element, list):
            total += add_nested_elements(element)
        else:
            if isinstance(element, int):
                total += element
            else:
                raise ValueError
    return total

nested_list = [1, 2, [3, 4], [5, [6, 7], 8], 9]
print(add_nested_elements(nested_list))  


def middle(lst):
   
    if len(lst) < 2:
        raise ValueError
    else:
        return lst[1:-1]
    
lst = [5, 6, 7, 8]
print(middle(lst)) 
Leave a Comment