我需要得到“计算机”这个词。 将每个字母转换为相应的ASCII值(使用For循环)。 然后将各个ASCII值一起添加以获得总和。
我在启动程序时输入“COMPUTER”作为“text”的参数。
所以想想COMPUTER = text
到目前为止我有什么:
def addASCIIValues(text): for char in text: AsciiArray = ord(char) print AsciiArray文本输入为'COMPUTER' 对于“计算机”中的每个角色 将每个字符转换为其ASCII值 将值一起添加 收到总和 打印总和
请使用没有导入功能的简单代码。
我正在使用Jython,但python响应也没问题!
I need to get the word "COMPUTER". Convert each letter to its corresponding ASCII value (using For Loop). Then add the individual ASCII values together to get a sum.
I am entering "COMPUTER" as a parameter for 'text' when I launch the program.
So think COMPUTER = text
What i have so far:
def addASCIIValues(text): for char in text: AsciiArray = ord(char) print AsciiArrayText is entered as 'COMPUTER' for every character in "COMPUTER" Convert every character to its ASCII Value Add the values together Receive a total SUM Print sum
Please use simple code with no import functions.
I am using Jython but python responses would be ok aswell!
最满意答案
你的一部分困惑可能是因为你已经命名了ord() AsciiArray返回的int 。 它不是“阵列”。
使用列表解析很简单:
word = 'COMPUTER' print sum([ord(c) for c in word])但是,您要求具体步骤:
word = 'COMPUTER' SUM = 0 for char in word: value = ord(char) SUM += value print SUM我应该指出,通常大写的名称用于常量,因此名称SUM通常不被认为是良好的做法。
Part of your confusion might be because you have named the int returned by ord() AsciiArray. It is not an "array".
This is simple using a list comprehension:
word = 'COMPUTER' print sum([ord(c) for c in word])However you asked for specific steps:
word = 'COMPUTER' SUM = 0 for char in word: value = ord(char) SUM += value print SUMI should point out that generally UPPERCASE names are used for constants, and so the name SUM would not normally be considered good practice.
更多推荐
发布评论