好的,所以我正在编写一个python加密代码。代码比它需要的要长,所以我把它的一部分切换到函数。我一开始这样做,它就说找不到变量。为了解决这个问题,我将变量设置为全局。但是,它仍然不起作用,并给我错误:
Traceback (most recent call last):
File "C:/Users/Alex/Desktop/Encryptor.py", line 272, in <module>
startup()
File "C:/Users/Alex/Desktop/Encryptor.py", line 257, in startup
encrypt()
File "C:/Users/Alex/Desktop/Encryptor.py", line 44, in encrypt
maincrypt(2)
File "C:/Users/Alex/Desktop/Encryptor.py", line 12, in maincrypt
print newtext
UnboundLocalError: local variable 'newtext' referenced before assignment
这是我的代码:
letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def maincrypt(num):
print otext
print textlist
print newtextlist
print newtext
print times
print i
add = times + num
while add > 25:
add = add - 25
if ord(i) >= 65 and ord(i) <= 90:
newtext += letters[add].uppercase
else:
newtext += letters[add]
def encrypt():
global otext
global textlist
global newtextlist
global newtext
global times
global i
otext = raw_input("Text?")
textlist = []
newtextlist = []
newtext= ""
times = 0
for i in otext:
textlist.append(i)
for i in textlist:
if i == 'a' or i == 'A':
maincrypt(0)
elif i == 'b' or i == 'B':
maincrypt(1)
elif i == 'c' or i == 'C':
maincrypt(2)
elif i == 'd' or i == 'D':
maincrypt(3)
elif i == 'e' or i == 'E':
maincrypt(4)
elif i == 'f' or i == 'F':
maincrypt(5)
elif i == 'g' or i == 'G':
maincrypt(6)
elif i == 'h' or i == 'H':
maincrypt(7)
elif i == 'i' or i == 'I':
maincrypt(8)
elif i == 'j' or i == 'J':
maincrypt(9)
elif i == 'k' or i == 'K':
maincrypt(10)
elif i == 'l' or i == 'L':
maincrypt(11)
elif i == 'm' or i == 'M':
maincrypt(12)
elif i == 'n' or i == 'N':
maincrypt(13)
elif i == 'o' or i == 'O':
maincrypt(14)
elif i == 'p' or i == 'P':
maincrypt(15)
elif i == 'q' or i == 'Q':
maincrypt(16)
elif i == 'r' or i == 'R':
maincrypt(17)
elif i == 's' or i == 'S':
maincrypt(18)
elif i == 't' or i == 'T':
maincrypt(19)
elif i == 'u' or i == 'U':
maincrypt(20)
elif i == 'v' or i == 'V':
maincrypt(21)
elif i == 'w' or i == 'W':
maincrypt(22)
elif i == 'x' or i == 'X':
maincrypt(23)
elif i == 'y' or i == 'Y':
maincrypt(24)
elif i == 'z' or i == 'Z':
maincrypt(25)
else:
newtext += i
times += 1
print newtext
def decrypt():
otext = raw_input("Text?")
textlist = []
newtextlist = []
newtext= ""
times = 0
for i in otext:
textlist.append(i)
for i in textlist:
if i == 'a' or i == 'A':
add = 0 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'b' or i == 'B':
add = 1 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'c' or i == 'C':
add = 2 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'd' or i == 'D':
add = 3 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'e' or i == 'E':
add = 4 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'f' or i == 'F':
add = 5 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'g' or i == 'G':
add = 6 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'h' or i == 'H':
add = 7 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'i' or i == 'I':
add = 8 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'j' or i == 'J':
add = 9 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'k' or i == 'K':
add = 10 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'l' or i == 'L':
add = 11 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'm' or i == 'M':
add = 12 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'n' or i == 'N':
add = 13 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'o' or i == 'O':
add = 14 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'p' or i == 'P':
add = 15 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'q' or i == 'Q':
add = 16 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'r' or i == 'R':
add = 17 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 's' or i == 'S':
add = 18 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 't' or i == 'T':
add = 19 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'u' or i == 'U':
add = 20 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'v' or i == 'V':
add = 21 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'w' or i == 'W':
add = 22 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'x' or i == 'X':
add = 23 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'y' or i == 'Y':
add = 24 - times
while add < 0:
add = add + 25
newtext += letters[add]
elif i == 'z' or i == 'Z':
add = 25 - times
while add < 0:
add = add + 25
newtext += letters[add]
else:
newtext += i
times += 1
print newtext
def startup():
upass = raw_input("Password: ")
if upass == "astrocrypt":
print """Welcome!
1: Encrypt
2: Decrypt
3: Credits
4: Quit"""
try:
choice = input()
except SyntaxError:
print "Oops! That's not a valid number! Try again!"
startup()
except NameError:
print "Oops! That's not a valid number! Try again!"
startup()
if choice == 1:
encrypt()
startup()
elif choice == 2:
decrypt()
startup()
elif choice == 3:
print "Made by Alex Taber"
startup()
elif choice == 4:
pass
else:
print "Unknown number"
startup()
else:
print "Incorrect password!"
startup()