【问题标题】:Output the sum of the ASCII values for each character of the input in Python [closed]输出Python中输入的每个字符的ASCII值的总和[关闭]
【发布时间】:2021-02-06 21:40:59
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

  • 我不确定我是否有足够的信息。你得到什么错误?如果总和代码是您遇到的问题,发布可能会有所帮助。
  • @Viper:冷,你展示了你是如何进行求和的?指出你犯错的地方会有所帮助。

标签: python loops input sum ascii


【解决方案1】:

您可以使用内置的 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]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2021-02-06
    • 2012-09-11
    • 2011-12-10
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多