加密程序 - 我不明白为什么它会抛出这些错误

时间:2018-04-06 07:25:25

标签: python python-3.x

我在python中使用简单的密码创建加密程序,反转字符串并在字母表中向前移3个字母。

original = input('Enter a phrase: ')
actual_word = original[::-1]

def mid(s, offset, amount):
    return s[offset:offset+amount]

def encrypt(word):
    for i in len(word):
        newtxt = mid(word, i, 1)
        newtxt = chr(ord(newtxt)+3)
        coded = coded + newtxt
    return coded

encrypted = encrypt(original)
print(encrypted)
input('Press ENTER to exit')

但是我收到这些错误,我不明白为什么会得到它们:

Traceback (most recent call last):
  File "C:\Users\MHT\Desktop\Python\Kryptering\Encryption.py", line 14, in <module>
    encrypted = encrypt(original)
  File "C:\Users\MHT\Desktop\Python\Kryptering\Encryption.py", line 8, in encrypt
    for i in len(word):
TypeError: 'int' object is not iterable

1 个答案:

答案 0 :(得分:0)

呼叫不应该是

for i in range(len(word))

len只返回单词

的长度