我想創建一個簡單的程序來加密一個字符串並返回其加密的字符。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