我想创建一个简单的程序来加密一个字符串并返回其加密的字符。For循环问题与加密脚本
但是我有for循环,这是造成Python来显示错误的问题:
Traceback (most recent call last):
File "C:/Users/Alex/Desktop/Uni/Programming/encrypt", line 18, in <module>
encrypt(encin)
File "C:/Users/Alex/Desktop/Uni/Programming/encrypt", line 12, in encrypt
encout += e6
UnboundLocalError: local variable 'encout' referenced before assignment
下面是代码:
key = 10
encout = ''
def encrypt(s):
for c in s:
if c != ' ' :
e1 = ord(s)
e2 = e1 - 97
e3 = e2 + key
e4 = e3 % 26
e5 = e4 + 97
e6 = chr(e5)
encout = encout + e6
else:
encout = encout + c
a = input("To encrypt a string type 1, to decrypt a string type 2: ")
if a == '1':
encin = input("Please type the string to encrypt: ")
encrypt(encin)
print(encout)
你可以看到它的任何问题?
谢谢。
感谢您的回答,我将使用这个,因为我不喜欢使用'全局安全'。 – user2975192