用户输入 = 我的电子邮件 ID 是 harry @ hogwarts.com 输出 = 我的电子邮件 ID 是 xx @ hogwarts.com
这是我到目前为止
def main():
message = []
userInput = str(input("Enter the sentence: "))
splitInput = str(list(userInput))
print(splitInput)
for item in splitInput:
indeces = splitInput.index('@')
while((indeces-1).isalpha()):
item = 'x'
message.append(item)
print(' '.join(message))
这是我得到的错误
File "C:\Users\Manmohit\Desktop\purifier.py", line 8, in main
while((indeces-1).isalpha()):
AttributeError: 'int' object has no attribute 'isalpha'
我已经尝试在网上寻找不同的方法。我想要类似的东西是 alpha 方法。我应该编写自己的 alpha 方法来检查还是可以使用内置的东西???帮助不胜感激。谢谢
更新:
将循环while((indeces-1).isalpha()):
更改为while((str(indeces-1)).isalpha()):
我没有得到错误,但我也没有得到任何输出。

您可以使用此功能来编码电子邮件:
>>> def encodeemail(email):
e = email.split("@")
return "@".join(["x" * len(e[0]), e[1]])
>>> encodeemail("harry@hogwarts.com")
xx@hogwarts.com
或者甚至
>>> def encodeemail(email):
d = email.split(" ")
for i, f in enumerate(d):
e = f.split("@")
if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
return " ".join(d)
>>> encodeemail("this is harry@hogwarts.com")
this is xx@hogwarts.com
不带枚举:
>>> def encodeemail(email):
d = email.split(" ")
for i in range(len(d)):
e = d[i].split("@")
if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
return " ".join(d)

如果字符串不仅包含电子邮件,您可以使用re
模块。
>>> s
'my email id is harry@hogwards.com'
>>> re.sub('(?<=\s)\w+(?=@)', lambda y: 'x'*len(y.group()), s)
'my email id is xx@hogwards.com'
我假设你想检查字符串的一部分是否通过或未通过 isalpha()测试。
虽然可能有更好的方法来解决你的问题,你也可以让你的代码工作。
while((indeces-1).isalpha()):
indeces 是一个整数,因为是-1,所以你应用 isalpha 的结果是一个 int,因为错误说。
(str(indeces-1)).isalpha()
这也不起作用。这是一个字符串的 int,所以 str(2)的结果是“2”,这不是你想要的测试。
如果你想检查字符,只是索引到字符串,像这样:
>>> for i in range(len(s)):
... if s[i].isalpha():
... print 'x'
... else:
... print s[i]
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(8条)