我必须用空格打印姓名,你能帮我吗? 我得到的代码是这样的:
class Perfil:
def __init__(self,email,nome,cidade):
self.email=email
self.nome=nome
self.cidade=cidade
def __str__(self):
return "Perfil de "+self.nome+" ""("+self.email+")"" de "+self.cidade
def getCidade(self):
return self.cidade
def setCidade(self,novo):
self.cidade=novo
def getDominio(self):
t=self.email.rpartition("@")
return t[2]
def limpaNome(self):
new=""
if self.nome.isalpha()==True:
return self.nome
else:
for i in self.nome:
if i.isalpha()==True:
new +=i
return new
当我运行程序时:
>>> p=Perfil("lol@mail.pt","Ze Car231los", "Porto")
>>> p.limpaNome()
'ZeCarlos'
我需要像“Ze Carlos”这样的印刷品(带空格)
基本上,我需要编写一个程序使用抽象数据类型(类配置文件)来保存每个用户的信息。每个对象都具有以下属性:
email
name
city
类应该有以下方法来操作上面的对象
Method
__init__(self, email, name, city) - constructor
__str__(self)
getCity(self) - return the value of atribute city
getCity(self.new) - return the atribute city with a new value
getDomain(self) - example: lol@mail.com sugestion: use the method partition (i have to return mail.com only)
cleanName(self) - change the atribute name, deleting characters WICH are not alphabetic or spaces sugestion: use method isalpha
或使用正则表达式:
请注意,如果我是你,我会:
如果您只想从字符串中删除所有出现的
'0'
、'1'
、'2'
、'9'
,则可以这样使用str.translate
:请注意,在Python中不需要这样的getter/setter:
相反,只需让用户直接访问/设置属性:
self.cidade
。如果在某个时刻,您希望在访问或分配属性时运行函数,那么您可以使cidade
成为property,而无需更改用法语法。你知道吗您甚至可以创建
getDominio
和limpaNome
属性:请注意,在访问或设置属性时不需要parethese。语法看起来就像
lipaNome
是一个普通属性:相关问题 更多 >
编程相关推荐