我正在写一个python程序来加密和解密一个文件。我的代码中的所有公式都应该可以正常工作,但我在实际循环中遇到了问题。我想向用户提供加密、解密或退出的选项。一旦他们作出选择,并通过了这个过程,我想程序自动问他们同样的问题,直到他们选择退出程序。你知道吗
我很感激你给我的任何建议,告诉我什么是我能解决的,什么是我可能做错的。你知道吗
编辑:为了澄清,我现在的代码将继续只循环用户最初选择的内容。它不给他们选择加密或解密连续,直到他们退出。你知道吗
以下是负责环路的主要功能:
def main():
print("Welcome to the Vigenere-cipher Encryption/Decryption Program\n")
validStrings = ['e', 'd', 'x']
userInput = input("Would you like to (e)ncrypt a file, (d)ecrypt a file, or e(x)it (enter e, d, or x)? ")
while userInput not in validStrings:
userInput = input("Sorry, that's an invalid choice. Please enter only e, d, or x: ")
if userInput == 'e':
while True:
path = input("Enter the text-file name to encrypt: ")
if osp.exists(path):
encrypt(path)
else:
print("Sorry the file", path, "does NOT exist -- please try again!")
elif userInput == 'd':
path = input("Enter the text-file name to decrypt: ")
if osp.exists(path):
fileName,fileExtension = osp.split(path)
fileName = fileName+".txt"
if osp.exists(fileName):
print("WARNING: The file '%s' already exists!" %fileName)
ch = input("Is it okay to wipe it out (y/n)? ")
if ch == 'n':
fileName = input("Enter the file name text that should be used (.txt extension will automatically be added) ")
fileName = fileName + ".txt"
elif ch == 'y':
pass
decrypt(path, fileName)
elif userInput == 'x':
print("Program Complete!")
return
好吧,似乎你需要做一些修改,而用户作出选择。在这里,我对您的代码片段进行了一些修改,请看一下:
这里所做的唯一更改是,在用户提供“x”作为输入之前,while循环将不允许用户执行任何其他操作。你知道吗
如果有用请告诉我。你知道吗
如果在程序中选择(e)ncrypt中的选项,则输入退出条件始终为true的
while
循环。在这个循环中,即使输入有效的文件名,循环也会继续。一旦输入了一个有效的文件名,就可以使用break
语句来解决这个问题在(d)ecrypt部分,如果用户输入预先存在的文件名,则提供覆盖选项。但是,如果用户再次输入预先存在的文件的名称,您可能会再次显示警告用户的消息。您可以将它放在一个循环中执行
break
语句,如果用户允许覆盖这个文件,比如你可以把整个菜单驱动的部分像
将文件名拆分为其名称和扩展名时,请使用
而不是
if
path
只存储文件名。你知道吗如果
path
是一条绝对路径或者你可以做些什么阅读
splitext()
here。你知道吗相关问题 更多 >
编程相关推荐