I want to raise TypeError when hotel visitor doesn't add valid email address
我想在酒店访客不添加有效的电子邮件地址时引发TypeError,但我收到此错误:
Traceback (most recent call last): File
"/Users/Bex/PycharmProjects/Programmes/OOP/Hotel.py", line 43, injane = Hotel.from_string("Jane,Doe,[email protected]") 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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: {} {} 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,[email protected]") print(jane.first_name) print(jane.last_name)`enter code here` print(jane.display_no_of_visitors()) print(jane.email) |
欢迎来到SO,Alex!
在你的
1 2 3 4 5 6 7 8 9 | 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 |
这似乎现在没有错误。
您还需要删除一些额外的代码以避免第11和46行的语法错误。(