我正在编写一个将加密和解密文件的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
答案 0 :(得分:1)
在用户做出选择时,您似乎需要进行一些修改。在这里,我修改了你的代码片段,看看:
def main():
print("Welcome to the Vigenere-cipher Encryption/Decryption Program\n")
validStrings = ['e', 'd', 'x']
while True :
userInput = input("Would you like to (e)ncrypt a file, (d)ecrypt a file, or e(x)it (enter e, d, or x)? ")
if(userInput not in validStrings)
userInput = input("Sorry, that's an invalid choice. Please enter only e, d, or x: ")
elif :
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!")
continue
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)
continue
elif userInput == 'x':
break
print("Program Complete!")
return
此处唯一的变化是,直到用户提供' x'作为输入,while循环不允许用户采取任何其他操作。
让我知道它是否有用。
答案 1 :(得分:1)
如果在程序中选择(e)ncrypt中的选项,则输入一个while
循环,其退出条件始终为真。在此循环中,即使输入有效的文件名,循环也会继续。一旦输入了有效的文件名,就可以使用break
语句来解决它,如
if userInput == 'e':
while True:
path = input("Enter the text-file name to encrypt: ")
if osp.exists(path):
encrypt(path)
print("Yeah!,1")
break
else:
print("Sorry the file", path, "does NOT exist -- please try again!")
在(d)ecrypt部分中,如果用户输入预先存在的文件的名称,则提供覆盖选项。但是,如果用户再次输入预先存在的文件的名称,您可能会再次显示警告用户的消息。您可以通过将其置于执行break
语句的循环中来执行此操作,如果用户提前覆盖文件,如
elif userInput == 'd':
path = input("Enter the text-file name to decrypt: ")
if osp.exists(path):
fileName,fileExtension = osp.split(path)
fileName = fileName+".txt"
print("Filename: ", fileName, "path: ", path)
while 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':
break
decrypt(path, fileName)
您可以将整个菜单驱动的部分放在像
这样的循环中while True:
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)
break
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"
print("Filename: ", fileName, "path: ", path)
while 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':
break
decrypt(path, fileName)
elif userInput == 'x':
print("Program Complete!")
return
将文件名拆分为其名称和扩展名时,请使用
fileName, fileExtension = osp.splitext(path)
而不是
fileName,fileExtension = osp.split(path)
如果path
只存储文件名。
如果path
是绝对路径或您可以做的事情
path,fullFileName = osp.split(path)
fileName, fileExtension = osp.splitext(fullFileName)
了解splitext()
here。