簡體   English   中英

使用if語句循環python程序

[英]Looping a python program with if statments

我正在編寫將加密和解密文件的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

嗯,似乎您需要在用戶做出選擇時進行一些修改。 在這里,我稍微修改了一下您的代碼片段:

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中選擇該選項, while進入while循環,其退出條件始終為true。 在此循環中,即使您輸入有效的文件名,循環也將繼續。 輸入有效的文件名后,您可以使用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() 信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM