class - Python :TypeError: this constructor takes no arguments -
when user enters email address, , program reads email , display according criteria (e.g yeo.myy@edu.co
), criteria:
username
yeo.myy
domain
edu.co
i know "@"
.
this code
class email: def __int__(self,emailaddr): self.emailaddr = emailaddr def domain(self): index = 0 in range(len(emailaddr)): if emailaddr[i] == "@": index = return self.emailaddr[index+1:] def username(self): index = 0 in range(len(emailaddr)): if emailaddr[i] == "@" : index = return self.emailaddr[:index] def main(): emailaddr = raw_input("enter email>>") user = email(emailaddr) print "username = ", user.username() print "domain = ", user.domain() main()
this error got:
traceback (most recent call last): file "c:/users/owner/desktop/sdsd", line 29, in <module> main() file "c:/users/owner/desktop/sdsd", line 24, in main user = email(emailaddr) typeerror: constructor takes no arguments
def __int__(self,emailaddr):
did mean __init__
?
def __init__(self,emailaddr):
you're missing couple self
s in methods, , return
s improperly indented.
def domain(self): index = 0 in range(len(self.emailaddr)): if self.emailaddr[i] == "@": index = return self.emailaddr[index+1:] def username(self): index = 0 in range(len(self.emailaddr)): if self.emailaddr[i] == "@" : index = return self.emailaddr[:index]
result:
username = yeo.myy domain = edu.co
incidentally, recommend partition
, rpartition
splitting string 2 pieces on given separator. sure beats keeping track of indices manually.
def domain(self): return self.emailaddr.rpartition("@")[2] def username(self): return self.emailaddr.rpartition("@")[0]
Comments
Post a Comment