TI/Skrypty z zajęć/k8a

Z Brain-wiki
# -*- coding: utf-8 -*-
"""
Created on Wed Jun  3 12:16:05 2020

@author: Tomek
"""

def inv(x):
    try:
        value = 1/x
        return value
    except (ZeroDivisionError,TypeError):
        print('podaj liczbe rozna od zera !!!!')
        return 0

#    finally:
#        print('oo')
        
class ArgumentCiaguError(Exception):
    pass

class CiagArytmetyczny():
    def __init__(self, a1, r):
        self.__a1 = float(a1)
        self.__r = float(r)

    def __get_a1__(self):
        return self.__a1

    def __get_r__(self):
        return self.__r
    
    def __set_a1__(self, a1):
        self.__a1 = float(a1)

    def __set_r__(self, r):
        self.__r = float(r)
    
    a1 = property(__get_a1__,__set_a1__)
    r = property(__get_r__,__set_r__)
    
    def __getitem__(self,n):
        if isinstance(n,int):
            if n < 1: raise ArgumentCiaguError('tylko naturalne bez zera') 
            return self.__a1 + (n-1)*self.__r
        raise TypeError
        
    def __add__(self, other):
        return CiagArytmetyczny(self.a1 + other.a1, self.r + other.r)

    def __str__(self):
        return 'CiagArytmetyczny a1 = '+str(self.__a1)+' r = '+str(self.__r)
    
    def __repr__(self):
        return str(self)
    
z = CiagArytmetyczny(1,1)
x = CiagArytmetyczny(100,1)

for n in range(-10,11):
    try:
        print(z[n])
    except ArgumentCiaguError:
        pass #print('zly argument')