尝试在python中创建加密/解密程序,从文件中读取消息,使用两个关键字加密,然后输出到另一个文件。此外,它可以反过来做到这一点。 我遇到了len()函数的问题: 我得到错误:在函数或方法中构建的类型的对象在此代码段中没有len():
if (len(line)) == 0:
fileRead = True
在上下文中,这是整个计划:
print("Welcome to Python Encrypt 2015")
print("Only enter alphabet charcters into the file - no punctuation!")
#repeat loop for using program again and so on...
finished = False
while not finished:
#input and validation of encrypt/decrypt input
option = input("Do you want to encrypt or decrypt? (E/D): ")
while "E" != option != "D":
option = input("Only input 'E' or 'D'! Do you want to encrypt or decrypt? (E/D): ")
#file to be encrypted/decrypted is opened to read
message = ""
fileRead = False
if option == "E":
file = open("Task3Decrypted.txt","r")
else:
file = open("Task3Encrypted.txt","r")
#message from 'to encrypt' file is read and outputted to variable
while not fileRead:
line = (file.readline()).strip()
if line.isalpha()==True:
line = line.lower
else:
input("Message contains numbers or symbols. Please correct! Press any key to continue: ")
sys.exit
if (len(line)) == 0:
fileRead = True
else:
message = message + line
#file closed - saving it
file.close()
#validating keyword and stretching and cutting to length of message
key1True = False
while key1True == False:
keyword1 = input("Enter your first keyword: ")
if keyword1.isalpha()==True:
keyword1 = keyword1.lower()
key1True = True
key1length = len(keyword1)
while len(keyword1)<len(message):
keyword1=keyword1+keyword1
keyword1=keyword1[:len(message)]
key2True = False
while key2True == False:
keyword2 = input("Enter your second keyword: ")
if keyword2.isalpha()==True:
keyword2 = keyword2.lower()
key2True = True
key2length = len(keyword2)
while len(keyword2)<len(message):
keyword2=keyword2+keyword2
keyword2=keyword2[:len(message)]
newMessage = ""
for i in range(len(message)):
char = ord(message[i])-96
key1 = ord(keyword1[i])-96
key2 = ord(keyword2[i])-96
if char==-64:
newMessage = newMessage+" "
else:
if option == "E":
#clearing original file for security
clearFileE = open("Task3Decrypted.txt","w")
clearFileE.close
#encryption
if char+key1+key2>26:
newMessage = newMessage+chr(char+key1+key2-26+96)
else:
newMessage = newMessage+chr(char+key1+key2+96)
newFile = open("Task3Encrypted.txt","w")
newFile.write(newMessage)
newFile.close()
else:
clearFileD = open("Task3Encrypted.txt","w")
clearFileD.close
#decryption
if char-key1-key2<1:
newMessage = newMessage+chr(char-key1-key2+26+96)
else:
newMessage = newMessage+chr(char-key1-key2+96)
newFile = open("Task3Decrypted.txt","w")
newFile.write(newMessage)
newFile.close()
carryOn = input("Do you want to encrypt/decrypt another message? (Y/N): ")
while "Y" != carryOn != "N":
carryOn = input("Only input 'Y' or 'N'! Do you want to encrypt/decrypt another message? (Y/N): ")
if carryOn == "N":
finished = True
提前致谢!
答案 0 :(得分:0)
你遗漏了
中的括号line = line.lower()
如果没有(),则设置为lower()函数本身的对象,因此出现错误消息。