charkey = input('\nWhat is the eight character key that was used in encryption?\n')
for i in charkey:
c = (ord(i))
print('Asc2num',c)
Output:
What is the eight character key that was used in encryption?
(*&^%$
Asc2num 40
Asc2num 42
Asc2num 38
Asc2num 94
Asc2num 37
Asc2num 36
我现在需要将这些数字加在一起,我已经尝试了总和(c),但它不起作用。
答案 0 :(得分:0)
尝试使用额外的变量
charkey = input('\nWhat is the eight character key that was used in encryption?\n')
res = 0
for i in charkey:
c = (ord(i))
res += c
print(res)
也是较短的版本:
print(sum((ord(x) for x in charkey)))
答案 1 :(得分:0)
sum()将列表或任何可迭代对象作为参数。
<强>演示:强>
例外:
In [105]: sum(1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-105-5771d20eddf7> in <module>()
----> 1 sum(1)
TypeError: 'int' object is not iterable
有效:
In [106]: sum([1,2,3,4,5])
Out[106]: 15
为添加创建变量:
<强>演示:强>
In [108]: add = 0
In [109]: for i in range(5):
.....: add = add + i
.....:
In [110]: add
Out[110]: 10
答案 2 :(得分:0)
我试过你的问题。这是一个解决方案
让
c=[]
for i in charkey:
c.append(ord(i))
sum(c)
>>> charkey = raw_input('\nWhat is the eight character key that was used in encryption?\n')
What is the eight character key that was used in encryption?
!@#$%^&*
>>> c
[33, 64, 35, 36, 37, 94, 38, 42]
>>> sum(c)
379