我正在做一个程序,把文件的内容加密成密文。我的问题是,当我的程序要求用户输入他们想要加载的文件名,而用户没有给出有效的响应时,就会出现“FileNotFoundError:”。我希望我的程序有一个功能,如果用户没有给出有效的响应,程序将继续告诉用户重试。在
def EncryptCode():
encryptFileLoad = input("Name the file and directory you want to load with the ending '.txt':\n")
with open (encryptFileLoad,mode="r",encoding="utf=8") as encrypt_file:
encryptFile = encrypt_file.read()
我得到这样一个错误:
^{pr2}$我试过这样做:
def EncryptCode():
...
try:
...
except FileNotFoundError:
return EncryptCode
怎么办
你差点就成功了。检查http://www.python-course.eu/recursive_functions.php应该是这样的:
或者您可以使用
^{pr2}$while
循环来要求用户输入正确的文件名,例如:相关问题 更多 >
编程相关推荐