我正在用python 3编写程序,将输入字符串转换为整数。代码只有一个问题。只要有空格,它就会打印-64。我试过编辑代码,但它与空格一起输出-64。有什么建议吗?
n = input("please enter the text:").lower()
print(n)
a = []
for i in n:
a.append(ord(i)-96)
if (ord(i)-96) == -64:
a.append(" ")
print(a)
谢谢
Input: "BatMan is Awesome"
Output: [2, 1, 20, 13, 1, 14, -64, ' ', 9, 19, -64, ' ', 1, 23, 5, 19, 15, 13, 5]
如果我对您的理解正确,则希望转换"abc def"
为[1, 2, 3, " ", 4, 5, 6]
。当前,您首先要添加ord(i) - 96
到列表中,然后,如果字符是空格,则添加一个额外的空格。ord(i) - 96
如果不是空格,则只想添加。
n = input("please enter the text:").lower()
print(n)
a = []
for i in n:
if (ord(i)-96) == -64:
a.append(" ")
else:
a.append(ord(i)-96)
print(a)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句