AttributeError: 'Punto' object has no attribute 'x'



In [44]:


In [44]: a

Traceback (most recent call last):


File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 702, in __call__

printer.pretty(obj)


File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py", line 400, in pretty

return _repr_pprint(obj, self, cycle)


File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py", line 695, in _repr_pprint

output = repr(obj)


File "C:/Users/Studente/Downloads/punto.py", line 21, in __repr__

return f'punto(x={self.x},y={self.y})'


AttributeError: 'Punto' object has no attribute 'x'



In [45]:


In [45]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [46]: d = Punto(-1,'ciao')

Traceback (most recent call last):


File "<ipython-input-46-eea6b6222bf8>", line 1, in <module>

d = Punto(-1,'ciao')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [47]:


In [47]: d

Out[47]: {}


In [48]: f = Punto(-1,'ciao')

Traceback (most recent call last):


File "<ipython-input-48-f8d04363da10>", line 1, in <module>

f = Punto(-1,'ciao')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [49]:


In [49]: f

Traceback (most recent call last):


File "<ipython-input-49-a9fcd54b25e7>", line 1, in <module>

f


NameError: name 'f' is not defined



In [50]:


In [50]: s = 'abcadasecsad'


In [51]: for c in s:

    ...: d[c] += 1

    ...:

Traceback (most recent call last):


File "<ipython-input-51-2b796281600e>", line 2, in <module>

d[c] += 1


KeyError: 'a'



In [52]:


In [52]: for c in s:

    ...: try:

    ...: d[c] += 1

    ...: except:

    ...: d[c] = 1

    ...:


In [53]: d

Out[53]: {'a': 4, 'b': 1, 'c': 2, 'd': 2, 's': 2, 'e': 1}


In [54]: for c in s:

    ...: try:

    ...: d[c] += 1

    ...: except KeyError:

    ...: d[c] = 1

    ...:


In [55]: d = {}


In [56]: for c in s:

    ...: try:

    ...: d[c] += 1

    ...: except KeyError:

    ...: d[c] = 1


In [57]: d

Out[57]: {'a': 4, 'b': 1, 'c': 2, 'd': 2, 's': 2, 'e': 1}


In [58]: for c in s:

    ...: try:

    ...: d[c] += 1/0

    ...: except KeyError:

    ...: d[c] = 1

Traceback (most recent call last):


File "<ipython-input-58-a88ac408de8b>", line 3, in <module>

d[c] += 1/0


ZeroDivisionError: division by zero



In [59]:


In [59]: for c in s:

    ...: try:

    ...: d[c] += 1/0

    ...: except:

    ...: d[c] = 1

    ...:


In [60]: d

Out[60]: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 's': 1, 'e': 1}


In [61]: for c in s:

    ...: try:

    ...: d[c] += 1/0

    ...: except:

    ...: d[c] = 1

    ...:


In [62]: d ={}


In [63]: for c in s:

    ...: try:

    ...: d[c] += 1/0

    ...: except:

    ...: d[c] = 1

    ...:


In [64]: s

Out[64]: 'abcadasecsad'


In [65]: d

Out[65]: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 's': 1, 'e': 1}


In [66]: d ={}


In [67]: for c in s:

    ...: try:

    ...: d[c] += 1

    ...: except:

    ...: d[c] = 1


In [68]: d

Out[68]: {'a': 4, 'b': 1, 'c': 2, 'd': 2, 's': 2, 'e': 1}


In [69]: for c in s:

    ...: try:

    ...: d[c] += 1

    ...: except:

    ...: d[c] = 1

    ...:


In [70]: x = Punto('a','a')

Traceback (most recent call last):


File "<ipython-input-70-7481d1fa6de2>", line 1, in <module>

x = Punto('a','a')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [71]:


In [71]: try:

    ...: x = Punto('a','a')

    ...: except:

    ...: x = Punto(0,0)

Traceback (most recent call last):


File "<ipython-input-71-7cef6322d3ad>", line 4, in <module>

x = Punto(0,0)


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [72]:


In [72]: try:

    ...: x = Punto('a','a')

    ...: except:

    ...: x = Punto(0.0,0.0)

    ...:


In [73]: x

Out[73]: punto(x=0.0,y=0.0)


In [74]: x=Punto(1,1)

Traceback (most recent call last):


File "<ipython-input-74-6d25c65c210e>", line 1, in <module>

x=Punto(1,1)


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [75]:


In [75]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [76]: x = Punto(1,1)

Traceback (most recent call last):


File "<ipython-input-76-c1022880db86>", line 1, in <module>

x = Punto(1,1)


File "C:/Users/Studente/Downloads/punto.py", line 10, in __init__

if any (type(x) == float, type(y) == float, type(x) == int, type(y) == int):


TypeError: any() takes exactly one argument (4 given)



In [77]:


In [77]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [78]: x = Punto(1,1)


In [79]: x

Out[79]: punto(x=1,y=1)


In [80]: x = Punto(1,'a')


In [81]: x

Out[81]: punto(x=1,y=a)


In [82]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [83]: x = Punto(1,'a')

Traceback (most recent call last):


File "<ipython-input-83-7f4e98fa66ea>", line 1, in <module>

x = Punto(1,'a')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float')


TypeError: Errore! x e y devono essere float



In [84]:


In [84]: x = Punto(1,0.2)

Traceback (most recent call last):


File "<ipython-input-84-defa23093f38>", line 1, in <module>

x = Punto(1,0.2)


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float o int')


TypeError: Errore! x e y devono essere float



In [85]:


In [85]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [86]: x = Punto(1,0.2)


In [87]: x = Punto(1,1)


In [88]: x = Punto(1,'a')

Traceback (most recent call last):


File "<ipython-input-88-7f4e98fa66ea>", line 1, in <module>

x = Punto(1,'a')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float o int')


TypeError: Errore! x e y devono essere float o int



In [89]:


In [89]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [90]: x = Punto(1,'a')

Traceback (most recent call last):


File "<ipython-input-90-7f4e98fa66ea>", line 1, in <module>

x = Punto(1,'a')


File "C:/Users/Studente/Downloads/punto.py", line 14, in __init__

raise TypeError ('Errore! x e y devono essere float o int')


TypeError: Errore! x e y devono essere float o int



In [91]:


In [91]: x = Punto(1,1)


In [92]: x = Punto(1,1.0)


In [93]: def funzione_pesantissima(a):

    ...: pass

    ...:

    ...:


In [94]: def funzione_pesantissima(a):

    ...: print('eseguo')

    ...:


In [95]: if True or funzione_pesantissima(5):

    ...: pass

    ...:

    ...:


In [96]: isinstance(a, Punto)

Out[96]: False


In [97]: a = Punto(0,0)


In [98]: isinstance(a, Punto)

Out[98]: True


In [99]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [100]: a = Punto(0,0)


In [101]: a = Punto(0,1)


In [102]: a = Punto(0,1.0)


In [103]: a = Punto(3,1.0)


In [104]: a = Punto('3',1.0)


In [105]: a = Punto('3.8',1.0)


In [106]: a = Punto('3.8','asd')

Traceback (most recent call last):


File "<ipython-input-106-30a25ffc3bc7>", line 1, in <module>

a = Punto('3.8','asd')


File "C:/Users/Studente/Downloads/punto.py", line 17, in __init__

self.y = float(y)


ValueError: could not convert string to float: 'asd'



In [107]:


In [107]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [108]: a = Punto(2,5)


In [109]: b = Punto(-3, 5)


In [110]: a.distanza(b)

Out[110]: 5.0


In [111]: c = Punto(2.5,7)


In [112]: a.distanza(c)

Out[112]: 2.0615528128088303


In [113]: c.distanza(a)

Out[113]: 2.0615528128088303


In [114]: c.distanza(6)

Traceback (most recent call last):


File "<ipython-input-114-aaa2ee4b5595>", line 1, in <module>

c.distanza(6)


File "C:/Users/Studente/Downloads/punto.py", line 30, in distanza

return ((self.x - p.x)**2 + (self.y - p.y)**2)**0.5


AttributeError: 'int' object has no attribute 'x'



In [115]:


In [115]: c.distanza('a')

Traceback (most recent call last):


File "<ipython-input-115-c43a766865ed>", line 1, in <module>

c.distanza('a')


File "C:/Users/Studente/Downloads/punto.py", line 30, in distanza

return ((self.x - p.x)**2 + (self.y - p.y)**2)**0.5


AttributeError: 'str' object has no attribute 'x'



In [116]:


In [116]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [117]: c = Punto(2.5,7)


In [118]: c.distanza('a')


In [119]: runfile('C:/Users/Studente/Downloads/punto.py', wdir='C:/Users/Studente/Downloads')


In [120]: c = Punto(2.5,7)


In [121]: c.distanza('a')

Traceback (most recent call last):


File "<ipython-input-121-c43a766865ed>", line 1, in <module>

c.distanza('a')


File "C:/Users/Studente/Downloads/punto.py", line 33, in distanza

raise ValueError ('La distanza si calcola fra due punti')


ValueError: La distanza si calcola fra due punti



In [122]:


In [122]: try:

     ...: c.distanza('a')

File "<ipython-input-122-da0dfeab2fa4>", line 2

c.distanza('a')

^

SyntaxError: unexpected EOF while parsing



In [123]:


In [123]: try:

     ...: c.distanza('a')

     ...: except:

     ...: pass


In [124]: try:

     ...: c.distanza('a')

     ...: except:

     ...: print('errore gestito')

errore gestito


In [125]: try:

     ...: c.distanza('a')

     ...: except TypeError:

     ...: print('errore gestito')

Traceback (most recent call last):


File "<ipython-input-125-03a3f2b2fc68>", line 2, in <module>

c.distanza('a')


File "C:/Users/Studente/Downloads/punto.py", line 33, in distanza

raise ValueError ('La distanza si calcola fra due punti')


ValueError: La distanza si calcola fra due punti



In [126]:


In [126]: try:

     ...: c.distanza('a')

     ...: except Error as e:

     ...: print('errore gestito', e)

Traceback (most recent call last):


File "<ipython-input-126-1326fc7323ed>", line 3, in <module>

except Error as e:


NameError: name 'Error' is not defined



In [127]:


In [127]: try:

     ...: c.distanza('a')

     ...: except Exception as e:

     ...: print('errore gestito', e)