'while' 循环未按预期运行
'while' loop not behaving as expected
背景:
我制作了一个程序,它接受文本输入,应用加密(一个简单的密码)并将输出保存到一个列表中——如果需要的话。消息也可以解密。
程序通过选项菜单导航:
1.加密消息
2.查看加密信息
3.解密消息
为了允许程序的所有部分访问相同的已保存消息列表(变量),我将其写在 class 中。 class 中存在调用此列表的定义。
目前只写入了'encrypt a message'位。
问题:
用户决策流程由两个 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()
这总是正确的:
if save_choice == "Y" or "y":
因为它被解释为:
if (condition) or (condition):
第一个条件是 (save_choice == "Y")
第二个条件就是 ("y"),python 解释为 'True'
所以整个条件总是为真。
你的意思大概是:
if save_choice == "Y" or save_choice == "y":
或更好:
if save_choice.lower() == "y":
不是 python 程序员,但我认为 python 不允许 "implied" 主题用于 或 运算符
--> IF save_choice == "Y" 或 save_choice == "y" :
背景:
我制作了一个程序,它接受文本输入,应用加密(一个简单的密码)并将输出保存到一个列表中——如果需要的话。消息也可以解密。
程序通过选项菜单导航: 1.加密消息 2.查看加密信息 3.解密消息
为了允许程序的所有部分访问相同的已保存消息列表(变量),我将其写在 class 中。 class 中存在调用此列表的定义。
目前只写入了'encrypt a message'位。
问题:
用户决策流程由两个 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()
这总是正确的:
if save_choice == "Y" or "y":
因为它被解释为:
if (condition) or (condition):
第一个条件是 (save_choice == "Y")
第二个条件就是 ("y"),python 解释为 'True'
所以整个条件总是为真。
你的意思大概是:
if save_choice == "Y" or save_choice == "y":
或更好:
if save_choice.lower() == "y":
不是 python 程序员,但我认为 python 不允许 "implied" 主题用于 或 运算符
--> IF save_choice == "Y" 或 save_choice == "y" :