当Caesar Cypher Python中给出无效的键时,如何仅输出所需的错误消息?
我正在编写一个Python文件,该文件可以通过Python使用Caesar Cypher加密和解密文本文件。 这是我一直在写的代码:
import sys
import argparse
import string
parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="filein", help="input file")
parser.add_argument("-o", dest="fileout", help="output file")
parser.add_argument("-k", dest="keyin", help="key", type=int)
parser.add_argument("-m", dest="modein", help="mode", type=str) #e or d
# parse our arguments
args = parser.parse_args()
filein = args.filein
fileout = args.fileout
keyin = args.keyin
modein = args.modein
def cipher_cipher_using_lookup(text, key, characters = string.printable, mode = args.modein):
if key <=0 or key >(len(string.printable)-1):
print("The key must be between 1 and 99 inclusive")
return None
n = len(characters)
if mode == "e" or mode == "E":
key = key -0
elif mode == "d" or mode == "D":
key = n - key
else:
print("Invalid mode input. Input either 'e' or 'd'. Case insensitive")
table = str.maketrans(characters, characters[key:]+characters[:key])
translated_text = text.translate(table)
return translated_text
def fileCipher(filein, fileout, keyin, modein):
with open (args.filein, "r") as f_in:
with open(args.fileout, "w") as f_out:
for line in f_in:
lineNew = cipher_cipher_using_lookup(line, key = args.keyin, mode = args.modein) #no characters argument
if args.modein != "e" and args.modein != "E" and args.modein != "d" and args.modein != "D":
break
f_out.write(lineNew)
# print("The file {} has been translated successfully and saved to {}".format(filein, fileout))
fileCipher(filein, fileout, keyin, modein)
当范围1至99以外的键无效(例如负数,0,数字大于99)时,我只希望我创建的错误消息:键必须在之间。 1和99要输出的包含性
。但是,我得到的是:
The key must be between 1 and 99 inclusive
Traceback (most recent call last):
File "[*the python script filepath*]", line 63, in <module>
fileCipher(filein, fileout, keyin, modein)
File "[*the python script filepath*]", line 59, in fileCipher
f_out.write(lineNew)
TypeError: write() argument must be str, not None
从cipher_cipher_uside_lookup()
删除无返回无,只会导致的大规模重复
必须在1到99含义,这也是不希望的。
当给出无效的密钥时,我仅如何输出必须在1到99包含
之间?
I am writing a python file that can encrypt and decrypt text files with Caesar cypher via python.
This is the code I have been writing:
import sys
import argparse
import string
parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="filein", help="input file")
parser.add_argument("-o", dest="fileout", help="output file")
parser.add_argument("-k", dest="keyin", help="key", type=int)
parser.add_argument("-m", dest="modein", help="mode", type=str) #e or d
# parse our arguments
args = parser.parse_args()
filein = args.filein
fileout = args.fileout
keyin = args.keyin
modein = args.modein
def cipher_cipher_using_lookup(text, key, characters = string.printable, mode = args.modein):
if key <=0 or key >(len(string.printable)-1):
print("The key must be between 1 and 99 inclusive")
return None
n = len(characters)
if mode == "e" or mode == "E":
key = key -0
elif mode == "d" or mode == "D":
key = n - key
else:
print("Invalid mode input. Input either 'e' or 'd'. Case insensitive")
table = str.maketrans(characters, characters[key:]+characters[:key])
translated_text = text.translate(table)
return translated_text
def fileCipher(filein, fileout, keyin, modein):
with open (args.filein, "r") as f_in:
with open(args.fileout, "w") as f_out:
for line in f_in:
lineNew = cipher_cipher_using_lookup(line, key = args.keyin, mode = args.modein) #no characters argument
if args.modein != "e" and args.modein != "E" and args.modein != "d" and args.modein != "D":
break
f_out.write(lineNew)
# print("The file {} has been translated successfully and saved to {}".format(filein, fileout))
fileCipher(filein, fileout, keyin, modein)
When invalid keys outside of the range 1 to 99 (e.g. negative numbers, 0, numbers larger than 99) I would only want the error message I have created: The key must be between 1 and 99 inclusive
to be outputted. However, I get this instead:
The key must be between 1 and 99 inclusive
Traceback (most recent call last):
File "[*the python script filepath*]", line 63, in <module>
fileCipher(filein, fileout, keyin, modein)
File "[*the python script filepath*]", line 59, in fileCipher
f_out.write(lineNew)
TypeError: write() argument must be str, not None
Removing return None
from cipher_cipher_using_lookup()
will only result in a massive repetition of The key must be between 1 and 99 inclusive
, which is undesired as well.
How do I only output The key must be between 1 and 99 inclusive
when an invalid key is given?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论