Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    31

    Problemi con l'istanziazione di oggetti in Python

    Ciao, ho un problema, non capisco come si fa ad istanziare gli oggetti e le classe in python.
    Seguo il libro Pensare da Informatico.

    Quello che mi dice il libro di fare è :
    codice:
    class Punto:													
            pass		
    	P1 = Punto()
    		P1.x = 3.0
    		P1.y = 4.0
    		print'(' + str(P1.x) +', '+ str(P1.y) + ')'
    		DistanzaAlQuadrato = P1.x * P1.x + P1.y * P1.y
    print P1.x
    print P1.y
    Il risultato è catastrofico, una sequenza di errori.

    codice:
    class Punto:                                                                                                   
         pass            
         P1 = Punto()
      File "<stdin>", line 2
        P1 = Punto()
        ^
    IndentationError: unexpected indent
    >>>             P1.x = 3.0
      File "<stdin>", line 1
        P1.x = 3.0
        ^
    IndentationError: unexpected indent
    >>>             P1.y = 4.0
      File "<stdin>", line 1
        P1.y = 4.0
        ^
    IndentationError: unexpected indent
    >>>             print'(' + str(P1.x) +', '+ str(P1.y) + ')'
      File "<stdin>", line 1
        print'(' + str(P1.x) +', '+ str(P1.y) + ')'
        ^
    IndentationError: unexpected indent
    >>>             DistanzaAlQuadrato = P1.x * P1.x + P1.y * P1.y
      File "<stdin>", line 1
        DistanzaAlQuadrato = P1.x * P1.x + P1.y * P1.y
        ^
    IndentationError: unexpected indent
    Se continuo a leggere arrivo al paragrafo 12.6, trovo un esempio, lo inserisco nel Terminale e mi dice :
    codice:
    def TrovaCentro(Rettangolo):
    	P = Punto()
    	P.x = Rettangolo.AltoSinistra.x + Rettangolo.Larghezza/2.0
    	P.y = Rettangolo.AltoSinistra.y + Rettangolo.Altezza/2.0
    	retunr P
    File "<stdin>", line 5
        retunr P
               ^
    SyntaxError: invalid syntax
    Ma che devo fare, non capisco più....

  2. #2
    Come ti dice Python stesso è un errore di indentazione.
    Prova così:

    codice:
    class Punto:													
        pass		
    
    P1 = Punto()
    P1.x = 3.0
    P1.y = 4.0
    print'(' + str(P1.x) +', '+ str(P1.y) + ')'
    DistanzaAlQuadrato = P1.x * P1.x + P1.y * P1.y
    print P1.x
    print P1.y
    Rilasciata Python FTP Server library 0.5.1
    http://code.google.com/p/pyftpdlib/

    We'll be those who'll make the italian folks know how difficult can be defecating in Southern California without having the crap flying all around the house.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.