一起添加ASCII值。(Adding ASCII Values together. Jython/Python)

编程入门 行业动态 更新时间:2024-10-27 02:25:11
一起添加ASCII值。(Adding ASCII Values together. Jython/Python)

我需要得到“计算机”这个词。 将每个字母转换为相应的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 AsciiArray
Text 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 SUM

I should point out that generally UPPERCASE names are used for constants, and so the name SUM would not normally be considered good practice.

更多推荐

本文发布于:2023-08-01 12:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Adding   ASCII   Values   Python   Jython

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!