我正在编写一个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发布于 2018-02-11 05:30:53
如果在程序中选择(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)项中,如果用户输入预先存在的文件的名称,则提供一个覆盖选项。但是,如果用户再次输入预先存在的文件的名称,您可以再次显示警告用户的消息。您可以通过将其放置在一个循环中来完成,如果用户允许继续重写文件,则在循环中执行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() 这里的文章。
发布于 2018-02-11 05:00:30
好吧,在用户做出选择时,您似乎需要做一些修改。在这里,我修改了您的代码片段,只需看一看:
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‘作为输入,而循环将不允许用户采取任何其他操作。
如果有用的话请告诉我。
https://stackoverflow.com/questions/48728329
复制相似问题