首页 > 解决方案 > “while”循环未按预期运行

问题描述

背景:

我制作了一个程序,它接受文本输入,应用加密(一个简单的密码)并将输出保存到列表中 - 如果需要。消息也可以被解密。

该程序通过选项菜单导航: 1. 加密消息 2. 查看加密消息 3. 解密消息

为了允许程序的所有部分访问保存消息的相同列表(变量),我在一个类中编写了它。在该类中存在调用此列表的 def。

到目前为止,仅写入了“加密消息”位。

问题:

用户决策流程有两个 Y/N 选择。但是,这些选择不起作用——即使用户输入了“N”——我的程序认为他们在这两种情况下都输入了“Y”。

def encrypt():
    def save_cip():#This function allows the user to save the ciphered message to the ciphered_messages if they choose
        choosing = True
        while choosing:
            save_choice = input("Would you like to save your Ciphered message? (Y/N)\n")
            if save_choice == "Y" or "y":
                print("You chose yes")
                cct.ciphered_messages.append(' '.join(["Message", str(len(cct.ciphered_messages)), ":", cipher]))
                choosing = False
            elif save_choice == "N" or "n":
                print("You chose no")
                choosing = False
                continue
            else:
                print("That was not a valid entry, please enter Y or N only")
                continue

我认为问题出在范围内,并且在设置和读取 Y 或 N 时以某种方式没有引用相同的变量。我已经在相同的代码上上下移动了大约 3 个小时但仍然没有,在许多中声明所有变量不同的地方没有运气,所以任何建议都非常感谢。

完整的可执行代码:

class cct:

print("Welcome to the CaeserCipher tool v1.0")
menu_state = "main" #This is used to record what state the program is in
unciphered_messages = [] #Decrypted messages are saved here
ciphered_messages = [] #Encrypted messages are saved here

def menu(): #This is the main menu interface
    while cct.menu_state == "main": #This while 
        cct.menu_state = input("What would you like to do? \n 1: Encrypt a Message \n 2: View Encrypted Messages \n 3: Decrypt a message\n")
        if cct.menu_state == "1":
            cct.encrypt()
        elif cct.menu_state == "2":
            cct.view()
        elif cct.menu_state == "3":
            cct.decrypt()
        elif cct.menu_state == "main":
            print("\n\nWelcome back to the menu!\n\n")
        else:
            print("You did not enter a valid choice. Please enter a number between 1 and 3.\n")
            cct.menu_state = "make_choice"
        continue

def encrypt():
    def save_cip():#This function allows the user to save the ciphered message to the ciphered_messages if they choose
        choosing = True
        while choosing:
            save_choice = input("Would you like to save your Ciphered message? (Y/N)\n")
            if save_choice == "Y" or "y":
                print("You chose yes")
                cct.ciphered_messages.append(' '.join(["Message", str(len(cct.ciphered_messages)), ":", cipher]))
                choosing = False
            elif save_choice == "N" or "n":
                print("You chose no")
                choosing = False
                continue
            else:
                print("That was not a valid entry, please enter Y or N only")
                continue

    #This while loop continually takes messages, gives the option of saving, and asks if you want to cipher another
    while cct.menu_state == "1":   
        text = input("Enter your message: ") #Enter the message you wish to cipher
        cipher = '' #Create a string for the cipher
        for char in text: #This for sub-loop will increment each character by 1. e.g. A -> B, T -> U, Z -> A
            if not char.isalpha():
                continue
            char = char.upper()
            code = ord(char) + 1
            if code > ord('Z'):
                code = ord('A')
            cipher += chr(code)
        print(' '.join(["Your ciphered message is:", cipher]))
        save_cip()
        print(cct.ciphered_messages)
        #This sub-while loop is reponsible for checking if you want to cipher another message
        #and making sure the user has made a valid choice
        choosing_another = True
        while choosing_another == True:
            choice_variable = input(' '.join(["You have", str(len(cct.ciphered_messages)), "saved messages \n", "Would you like to Cipher another? (Y/N)\n"]))
            if choice_variable == "Y" or "y":
                print("You chose yes")
                choosing_another = False
            elif choice_variable == "N" or "n":
                print("You chose no")
                cct.menu_state = "main"
                choosing_another = False
            else:
                choice_variable = input("That was not a valid entry, please enter Y or N only: \n")
                continue
    return


def view():
#TO BE CODED
    return

def decrypt():
    #TO BE CODED
    return

cct.menu()

标签: pythonloopswhile-loopscopemenu

解决方案


不是 python 程序员,但我不认为 python 允许or运算符的“隐含”主题
-> IF save_choice == "Y" OR save_choice == "y" :


推荐阅读