python 3-ord需要一个字符

python 3-ord需要一个字符,python,Python,我得到一个错误:ord需要一个字符。我认为这与我的for循环有关。这方面的任何帮助都将是巨大的 您确实应该更仔细地阅读错误消息。它实际上说的是 def toNumbers(strList): sum = 0 strList[:]= (ord(ch)-96 for ch in strList) print(strList) def main(): print("Modifies each entry in the list by converting it to

我得到一个错误:ord需要一个字符。我认为这与我的for循环有关。这方面的任何帮助都将是巨大的

您确实应该更仔细地阅读错误消息。它实际上说的是

def toNumbers(strList):
    sum = 0
    strList[:]= (ord(ch)-96 for ch in strList)
    print(strList)


def main():
    print("Modifies each entry in the list by converting it to a number.")
    strList = [ "dd", "t", "c"]
    nums= toNumbers(strList)
main()

长度为2的字符串是
“dd”

没有缩进的Python代码不是a。无法重现错误(也许我不应该修复缩进,但我怀疑它)这对我来说很好,并打印[4,20,3]这段代码应该演示问题吗?或者你的意思是你把它改成了别的东西,导致了一个错误?如果是这样,我可以用
strList=[“dd”、“t”、“c”]
Correct重现错误。但是,我希望我的代码接受额外的字符长度。@karan“dd”不是字符。函数仅适用于长度为1的字符串。除非你把那根线分成两条长度为1的线,否则这是行不通的。啊,好的,谢谢@RudiKershaw。有没有办法在不使用ord函数的情况下修改列表中的字符串?你不会说你想用2个字符的stirng做什么,但这会给你一些有用的东西:
strList=[[ord(ch)for ch in s]for s in strList]
@BoarGules谢谢,但我希望列表中的字符表示为一个值。e、 g.abc(1,2,3)等于6。
TypeError: ord() expected a character, but string of length 2 found