Questions about classes and methods in Python

0

I have a question, I can not do an exercise in classes and methods.

  

2 - Create the "Date" class with the attributes: day, month, and year. And create the methods "setarData (receive day, month and year)" and "print (" ex: 03/08/2017 ")". The builder should start on 01/01/1970. USE OPTIONAL PARAMETERS.

The program should start on 1/1/1970 and should print the date that the user entered. Ex: 08/03/2017

Image: link

  • UPDATED IMAGE (I can not import the class in the main program): link

What I was able to do, I caught in that part ...:

class Data:
def __init__(self, d = 1, m = 1, a = 1970):
    self.dia = d
    self.mes = m
    self.ano = a

def setarData(self, d = 3, m = 8, a = 2017):
    self.dia = 
    self.mes = m
    self.ano = a
    
asked by anonymous 04.08.2017 / 01:05

1 answer

1

It seems very simple, you are almost there. Just fix an error in the setarData() method, as noticed by Anderson ...

def setarData(self, d = 3, m = 8, a = 2017):
    self.dia = d
    self.mes = m
    self.ano = a

And add method imprimir() :

def imprimir(self):
    print '%02d/%02d/%04' % (self.dia, self.mes, self.ano)

If I were you, I would remove the optional parameters from the setarData() method because apparently it was not requested and I do not seem to make much sense either. It would look like this:

def setarData(self, d, m, a):
    self.dia = d
    self.mes = m
    self.ano = a
    
04.08.2017 / 03:20