输出 Python 中输入的每个字符的 ASCII 值之和

Output the sum of the ASCII values for each character of the input in Python

我找不到对 ASCII 数值求和的方法,我总是出错。

这是我的代码:

str = input("Enter your name: ")
for c in str:
    print("The ASCII value for ", c, "is", ord(c))

我删除了总结的部分,因为它是错误的。

您可以使用内置的 sum 和列表推导来尝试以下操作。作为一种好的做法,请记住不要使用内置类型或函数名称 (str) 定义变量。

name = input("Enter your name: ") 
for c in name: 
  print("The ASCII value for ", c, "is", ord(c))
print("The total sum is: ",sum([ord(c) for c in name]))