【发布时间】:2020-09-04 04:07:36
【问题描述】:
我尝试编写一个函数,它接收一个字符串,并打印编码文本,a' 为 1,'b' 为 2,...,'z' 为 26。输出应该是一个字符串,用“.”分隔的字母。例如,encode("Hello!") 应该打印出 "8.5.12.12.15.999"。这是我的代码,但它不起作用,我不知道为什么。当我运行代码时,最后没有打印任何内容。
def encode(text = input("Enter a text below please: ")):
tx = ""
text = text.lower()
text = "".join(text.split())
for x in range(0,len(text)):
conv_char = ord(text[x]) - 96
if conv_char > 0 and conv_char <= 26:
tx += str(conv_char) + "."
return(tx)
print(tx)
【问题讨论】:
标签: python-3.x string function numbers