When the user enters an email address, and the program reads the email and display it according to its criteria (e.g yeo.myy@edu.co
), like criteria:
username
isyeo.myy
domain
isedu.co
I know its something to do with the "@"
.
this is the code
class Email: def __int__(self,emailAddr): self.emailAddr = emailAddr def domain(self): index = 0 for i in range(len(emailAddr)): if emailAddr[i] == "@": index = i return self.emailAddr[index+1:] def username(self): index = 0 for i in range(len(emailAddr)): if emailAddr[i] == "@" : index = i return self.emailAddr[:index] def main(): emailAddr = raw_input("Enter your email>>") user = Email(emailAddr) print "Username = ", user.username() print "Domain = ", user.domain() main()
this is the error I 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: this constructor takes no arguments
Advertisement
Answer
def __int__(self,emailAddr):
Did you mean __init__
?
def __init__(self,emailAddr):
You’re also missing a couple self
s in your methods, and your return
s are improperly indented.
def domain(self): index = 0 for i in range(len(self.emailAddr)): if self.emailAddr[i] == "@": index = i return self.emailAddr[index+1:] def username(self): index = 0 for i in range(len(self.emailAddr)): if self.emailAddr[i] == "@" : index = i return self.emailAddr[:index]
Result:
Username = yeo.myy Domain = edu.co
Incidentally, I recommend partition
and rpartition
for splitting a string into two pieces on a 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]