Encrypting text with cesar cipher, str.replace gives TypeError: Can't convert 'NoneType' object to str implicitly
Encrypting text with cesar cipher, str.replace gives TypeError: Can't convert 'NoneType' object to str implicitly
cipher.py
:
import argparse
def parse_command_line():
parser=argparse.ArgumentParser()
parser.add_argument("infile",type=str,help="input file to be encrypted or decrypted")
parser.add_argument("-o outfile_path","--outfile outfile_path",type=str,help="output file")
parser.add_argument("-k KEY","--key KEY",type=int,default=1,help="encryption/decryption key (must be positive) (default= 1)")
parser.add_argument("-d","--decrypt",action="store_true",help="decrypt the input file")
parser.add_argument("-a","--all",action="store_false",help="decrypt using all keys [1, 25], save outputs in different files. (useful in case the key is lost orunknown)")
parser.add_argument("-v","--verbose",action="store_true",help="Verbose mode")
args=parser.parse_args()
return args
pass
def transform(message, key, decrypt):
#TODO: Your code goes here
if decrypt:
for i in message:
temp=shift(i,key)
transformed_message=message.replace(i,temp,1)
message=transformed_message
return transformed_message
pass
def shift(char, key):
# ordered lower case alphabet
alphabet = "abcdefghijklmnopqrstuvwxyz"
# will contain shifted lower case alphabet
shifted_alphabet = ''
for i in range(len(alphabet)):
shifted_alphabet = shifted_alphabet + alphabet[(i + key) % 26]
if char.isalpha():
char_index = alphabet.index(char.lower())
shifted_char = shifted_alphabet[char_index]
# keep char's case (upper or lower)
if char.isupper():
return shifted_char.upper()
else:
return shifted_char
def main():
# parse command line arguments
args = parse_command_line()
# key is specified
if not args.all:
# encrypt/decrypt content of infile
outstring = transform(instring, args.key, args.decrypt)
# write content of outstring to outfile
write_file(outstring, args.outfile)
# key is not specified, try all keys from 1 to 25 to decrypt infile
else:
for k in range(1, 26):
# decrypt content of infile
outstring = transform(instring, k, True)
# write content of outstring to outfile
write_file(outstring, "decrypted_by_" + str(k) + ".txt")
if __name__ == '__main__':
main()
我运行用以下方法解决这个问题:
$ python3 cipher.py plain_message.txt -k 23 -v -o cipher_message.txt
文件是:
Software is a great combination between artistry and engineering.
--Bill Gates
但这给出了以下回溯:
Traceback (most recent call last):
File "cipher.py", line 231, in <module>
main()
File "cipher.py", line 218, in main
outstring = transform(instring, k, True)
File "cipher.py", line 141, in transform
transformed_message=message.replace(i,temp,1)
TypeError: Can't convert 'NoneType' object to str implicitly
我试过运行transform(message,key, decrypt)
分开,returns串没问题。
请只检查 parse_command_line()
和 transform(message,key, decrypt)
,
因为 shift()
和 main()
是预先写好的,这些应该没有问题。
您的错误源于您的 shift()
函数在遇到文件第一行文本的最后一个字符时:
'Software is a great combination between artistry and engineering.'
.
不是 isalpha()
,因此 shift() 函数没有显式 return 任何暗示它 returns None
的东西。
您的转换方法无法正常处理 None
:
def transform(message, key, decrypt):
if decrypt:
for i in message:
temp=shift(i,key)
transformed_message=message.replace(i,temp,1) # does not work if temp == None
message=transformed_message
return transformed_message
pass
如果temp is None
:
不替换可以避免错误
def transform(message, key, decrypt):
if decrypt:
for i in message:
temp=shift(i,key)
if temp: # check if not Falsy - None is falsy, "if temp is not None:" works too
transformed_message=message.replace(i,temp,1)
# assigning something to message does nothing, so removed it
return transformed_message
如果允许您更改 shift() 函数本身,我可能只是 return 也可以工作的(未更改的)非映射字符。
cipher.py
:
import argparse
def parse_command_line():
parser=argparse.ArgumentParser()
parser.add_argument("infile",type=str,help="input file to be encrypted or decrypted")
parser.add_argument("-o outfile_path","--outfile outfile_path",type=str,help="output file")
parser.add_argument("-k KEY","--key KEY",type=int,default=1,help="encryption/decryption key (must be positive) (default= 1)")
parser.add_argument("-d","--decrypt",action="store_true",help="decrypt the input file")
parser.add_argument("-a","--all",action="store_false",help="decrypt using all keys [1, 25], save outputs in different files. (useful in case the key is lost orunknown)")
parser.add_argument("-v","--verbose",action="store_true",help="Verbose mode")
args=parser.parse_args()
return args
pass
def transform(message, key, decrypt):
#TODO: Your code goes here
if decrypt:
for i in message:
temp=shift(i,key)
transformed_message=message.replace(i,temp,1)
message=transformed_message
return transformed_message
pass
def shift(char, key):
# ordered lower case alphabet
alphabet = "abcdefghijklmnopqrstuvwxyz"
# will contain shifted lower case alphabet
shifted_alphabet = ''
for i in range(len(alphabet)):
shifted_alphabet = shifted_alphabet + alphabet[(i + key) % 26]
if char.isalpha():
char_index = alphabet.index(char.lower())
shifted_char = shifted_alphabet[char_index]
# keep char's case (upper or lower)
if char.isupper():
return shifted_char.upper()
else:
return shifted_char
def main():
# parse command line arguments
args = parse_command_line()
# key is specified
if not args.all:
# encrypt/decrypt content of infile
outstring = transform(instring, args.key, args.decrypt)
# write content of outstring to outfile
write_file(outstring, args.outfile)
# key is not specified, try all keys from 1 to 25 to decrypt infile
else:
for k in range(1, 26):
# decrypt content of infile
outstring = transform(instring, k, True)
# write content of outstring to outfile
write_file(outstring, "decrypted_by_" + str(k) + ".txt")
if __name__ == '__main__':
main()
我运行用以下方法解决这个问题:
$ python3 cipher.py plain_message.txt -k 23 -v -o cipher_message.txt
文件是:
Software is a great combination between artistry and engineering.
--Bill Gates
但这给出了以下回溯:
Traceback (most recent call last):
File "cipher.py", line 231, in <module>
main()
File "cipher.py", line 218, in main
outstring = transform(instring, k, True)
File "cipher.py", line 141, in transform
transformed_message=message.replace(i,temp,1)
TypeError: Can't convert 'NoneType' object to str implicitly
我试过运行transform(message,key, decrypt)
分开,returns串没问题。
请只检查 parse_command_line()
和 transform(message,key, decrypt)
,
因为 shift()
和 main()
是预先写好的,这些应该没有问题。
您的错误源于您的 shift()
函数在遇到文件第一行文本的最后一个字符时:
'Software is a great combination between artistry and engineering.'
.
不是 isalpha()
,因此 shift() 函数没有显式 return 任何暗示它 returns None
的东西。
您的转换方法无法正常处理 None
:
def transform(message, key, decrypt):
if decrypt:
for i in message:
temp=shift(i,key)
transformed_message=message.replace(i,temp,1) # does not work if temp == None
message=transformed_message
return transformed_message
pass
如果temp is None
:
def transform(message, key, decrypt):
if decrypt:
for i in message:
temp=shift(i,key)
if temp: # check if not Falsy - None is falsy, "if temp is not None:" works too
transformed_message=message.replace(i,temp,1)
# assigning something to message does nothing, so removed it
return transformed_message
如果允许您更改 shift() 函数本身,我可能只是 return 也可以工作的(未更改的)非映射字符。