用if语句循环python程序

2024-06-16 09:59:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个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

Tags: orthetopath程序inputifexists
2条回答

好吧,似乎你需要做一些修改,而用户作出选择。在这里,我对您的代码片段进行了一些修改,请看一下:

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循环将不允许用户执行任何其他操作。你知道吗

如果有用请告诉我。你知道吗

如果在程序中选择(e)ncrypt中的选项,则输入退出条件始终为true的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)

ifpath只存储文件名。你知道吗

如果path是一条绝对路径或者你可以做些什么

path,fullFileName = osp.split(path)
fileName, fileExtension = osp.splitext(fullFileName)

阅读splitext()here。你知道吗

相关问题 更多 >