I want to raise TypeError when hotel visitor doesn't add valid email address











up vote
0
down vote

favorite












I want to raise TypeError when hotel visitor doesn't add valid email address but I'm getting this error instead:




Traceback (most recent call last): File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 43, in

jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com") File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 15, in
from_string
return cls(first_name, last_name, email) File "/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 25, in
init
raise TypeError("You've entered an invalid email address.") TypeError: You've entered an invalid email address.




class Hotel:
visitors = 0
rooms = ['single', 'double', 'queen', 'king', 'executive']

@classmethod
def display_no_of_visitors(cls):
if Hotel.visitors > 0:
return "There are currently {} visitor(s)".format(Hotel.visitors)
else:
return "There are no visitors currently at the hotel"
enter code here
@classmethod
def from_string(cls, str):
first_name, last_name, email = str.split(",")
return cls(first_name, last_name, email)


def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name
for char in email:
if char in "@":
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1

@property
def email(self):
return self.email

def __repr__(self):
return "Full Name: {} {}n Email Address: {}".format(self.first_name, self.last_name, self.email)

def full_name(self):
return "{} {}".format(self.first_name, self.last_name)

def checkout(self):
Hotel.visitors -= 1
return "{} {} has checked out".format(self.first_name, self.last_name)


jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com")

print(jane.first_name)
print(jane.last_name)`enter code here`
print(jane.display_no_of_visitors())
print(jane.email)









share|improve this question
























  • It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
    – CJ59
    Nov 12 at 2:29










  • On line 11 you got enter code here which isn't commented!
    – Green Cell
    Nov 12 at 2:31















up vote
0
down vote

favorite












I want to raise TypeError when hotel visitor doesn't add valid email address but I'm getting this error instead:




Traceback (most recent call last): File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 43, in

jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com") File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 15, in
from_string
return cls(first_name, last_name, email) File "/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 25, in
init
raise TypeError("You've entered an invalid email address.") TypeError: You've entered an invalid email address.




class Hotel:
visitors = 0
rooms = ['single', 'double', 'queen', 'king', 'executive']

@classmethod
def display_no_of_visitors(cls):
if Hotel.visitors > 0:
return "There are currently {} visitor(s)".format(Hotel.visitors)
else:
return "There are no visitors currently at the hotel"
enter code here
@classmethod
def from_string(cls, str):
first_name, last_name, email = str.split(",")
return cls(first_name, last_name, email)


def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name
for char in email:
if char in "@":
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1

@property
def email(self):
return self.email

def __repr__(self):
return "Full Name: {} {}n Email Address: {}".format(self.first_name, self.last_name, self.email)

def full_name(self):
return "{} {}".format(self.first_name, self.last_name)

def checkout(self):
Hotel.visitors -= 1
return "{} {} has checked out".format(self.first_name, self.last_name)


jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com")

print(jane.first_name)
print(jane.last_name)`enter code here`
print(jane.display_no_of_visitors())
print(jane.email)









share|improve this question
























  • It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
    – CJ59
    Nov 12 at 2:29










  • On line 11 you got enter code here which isn't commented!
    – Green Cell
    Nov 12 at 2:31













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to raise TypeError when hotel visitor doesn't add valid email address but I'm getting this error instead:




Traceback (most recent call last): File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 43, in

jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com") File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 15, in
from_string
return cls(first_name, last_name, email) File "/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 25, in
init
raise TypeError("You've entered an invalid email address.") TypeError: You've entered an invalid email address.




class Hotel:
visitors = 0
rooms = ['single', 'double', 'queen', 'king', 'executive']

@classmethod
def display_no_of_visitors(cls):
if Hotel.visitors > 0:
return "There are currently {} visitor(s)".format(Hotel.visitors)
else:
return "There are no visitors currently at the hotel"
enter code here
@classmethod
def from_string(cls, str):
first_name, last_name, email = str.split(",")
return cls(first_name, last_name, email)


def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name
for char in email:
if char in "@":
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1

@property
def email(self):
return self.email

def __repr__(self):
return "Full Name: {} {}n Email Address: {}".format(self.first_name, self.last_name, self.email)

def full_name(self):
return "{} {}".format(self.first_name, self.last_name)

def checkout(self):
Hotel.visitors -= 1
return "{} {} has checked out".format(self.first_name, self.last_name)


jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com")

print(jane.first_name)
print(jane.last_name)`enter code here`
print(jane.display_no_of_visitors())
print(jane.email)









share|improve this question















I want to raise TypeError when hotel visitor doesn't add valid email address but I'm getting this error instead:




Traceback (most recent call last): File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 43, in

jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com") File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 15, in
from_string
return cls(first_name, last_name, email) File "/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 25, in
init
raise TypeError("You've entered an invalid email address.") TypeError: You've entered an invalid email address.




class Hotel:
visitors = 0
rooms = ['single', 'double', 'queen', 'king', 'executive']

@classmethod
def display_no_of_visitors(cls):
if Hotel.visitors > 0:
return "There are currently {} visitor(s)".format(Hotel.visitors)
else:
return "There are no visitors currently at the hotel"
enter code here
@classmethod
def from_string(cls, str):
first_name, last_name, email = str.split(",")
return cls(first_name, last_name, email)


def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name
for char in email:
if char in "@":
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1

@property
def email(self):
return self.email

def __repr__(self):
return "Full Name: {} {}n Email Address: {}".format(self.first_name, self.last_name, self.email)

def full_name(self):
return "{} {}".format(self.first_name, self.last_name)

def checkout(self):
Hotel.visitors -= 1
return "{} {} has checked out".format(self.first_name, self.last_name)


jane = Hotel.from_string("Jane,Doe,janedoe@gmail.com")

print(jane.first_name)
print(jane.last_name)`enter code here`
print(jane.display_no_of_visitors())
print(jane.email)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 2:24









sme

2,17621230




2,17621230










asked Nov 12 at 2:20









Alex-Sal91

102




102












  • It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
    – CJ59
    Nov 12 at 2:29










  • On line 11 you got enter code here which isn't commented!
    – Green Cell
    Nov 12 at 2:31


















  • It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
    – CJ59
    Nov 12 at 2:29










  • On line 11 you got enter code here which isn't commented!
    – Green Cell
    Nov 12 at 2:31
















It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
– CJ59
Nov 12 at 2:29




It looks like you are raising an unhandled TypeError? What do you want to do with that exception?
– CJ59
Nov 12 at 2:29












On line 11 you got enter code here which isn't commented!
– Green Cell
Nov 12 at 2:31




On line 11 you got enter code here which isn't commented!
– Green Cell
Nov 12 at 2:31












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Welcome to SO, Alex!



In your __init__ it is raising your TypeError since it's using a loop to check if every character is @, and since not every character is it fails. Instead you can drop the loop and just check it like so:



def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name

if "@" in email:
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1


This seems to work with no errors now.
You also need to remove some extra code to avoid syntax errors at lines 11 and 46. (the enter code here)






share|improve this answer





















  • I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
    – Alex-Sal91
    Nov 12 at 11:14












  • No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
    – Green Cell
    Nov 12 at 11:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255239%2fi-want-to-raise-typeerror-when-hotel-visitor-doesnt-add-valid-email-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Welcome to SO, Alex!



In your __init__ it is raising your TypeError since it's using a loop to check if every character is @, and since not every character is it fails. Instead you can drop the loop and just check it like so:



def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name

if "@" in email:
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1


This seems to work with no errors now.
You also need to remove some extra code to avoid syntax errors at lines 11 and 46. (the enter code here)






share|improve this answer





















  • I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
    – Alex-Sal91
    Nov 12 at 11:14












  • No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
    – Green Cell
    Nov 12 at 11:16















up vote
0
down vote



accepted










Welcome to SO, Alex!



In your __init__ it is raising your TypeError since it's using a loop to check if every character is @, and since not every character is it fails. Instead you can drop the loop and just check it like so:



def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name

if "@" in email:
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1


This seems to work with no errors now.
You also need to remove some extra code to avoid syntax errors at lines 11 and 46. (the enter code here)






share|improve this answer





















  • I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
    – Alex-Sal91
    Nov 12 at 11:14












  • No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
    – Green Cell
    Nov 12 at 11:16













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Welcome to SO, Alex!



In your __init__ it is raising your TypeError since it's using a loop to check if every character is @, and since not every character is it fails. Instead you can drop the loop and just check it like so:



def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name

if "@" in email:
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1


This seems to work with no errors now.
You also need to remove some extra code to avoid syntax errors at lines 11 and 46. (the enter code here)






share|improve this answer












Welcome to SO, Alex!



In your __init__ it is raising your TypeError since it's using a loop to check if every character is @, and since not every character is it fails. Instead you can drop the loop and just check it like so:



def __init__(self, first_name, last_name, email):
self.first_name = first_name
self.last_name = last_name

if "@" in email:
self.email = email
else:
raise TypeError("You've entered an invalid email address.")
Hotel.visitors += 1


This seems to work with no errors now.
You also need to remove some extra code to avoid syntax errors at lines 11 and 46. (the enter code here)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 2:36









Green Cell

1,818825




1,818825












  • I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
    – Alex-Sal91
    Nov 12 at 11:14












  • No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
    – Green Cell
    Nov 12 at 11:16


















  • I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
    – Alex-Sal91
    Nov 12 at 11:14












  • No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
    – Green Cell
    Nov 12 at 11:16
















I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
– Alex-Sal91
Nov 12 at 11:14






I've managed to raise the TypeError now adding the logic in my init. Thanks very much :)
– Alex-Sal91
Nov 12 at 11:14














No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
– Green Cell
Nov 12 at 11:16




No worries, if you think this has resolved your question please accept it so others know you're not looking for more answers.
– Green Cell
Nov 12 at 11:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255239%2fi-want-to-raise-typeerror-when-hotel-visitor-doesnt-add-valid-email-address%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine