Python 在while循环中将变量与输入进行比较时遇到问题
当我尝试学习Python时,我在编写一个基本程序时遇到了一些问题,问题是我试图将用户输入与我设置的变量进行比较,但当我尝试比较它们时,它不起作用 这就是所讨论的循环:Python 在while循环中将变量与输入进行比较时遇到问题,python,variables,Python,Variables,当我尝试学习Python时,我在编写一个基本程序时遇到了一些问题,问题是我试图将用户输入与我设置的变量进行比较,但当我尝试比较它们时,它不起作用 这就是所讨论的循环: if del_question == "1": symbol = input("What symbol would you like to change?: ") while len(symbol) != 1 or symbol not in words: print("Sorry, th
if del_question == "1":
symbol = input("What symbol would you like to change?: ")
while len(symbol) != 1 or symbol not in words:
print("Sorry, that is not a valid symbol")
symbol = input("What symbol would you like to change?: ")
letter = input("What would you like to change it to?: ")
while letter in words and len(letter) != 1:
print("Sorry, that is not a valid letter")
letter = input("What letter would you like to change?: ")
dictionary[symbol] = letter
words = words.replace(symbol, letter)
print("Here is your new code: \n", words)
这个游戏是关于通过字母和符号配对来破解密码的,这是字母和符号配对的地方,但在字母输入上,当我尝试配对时,你无法将同一个字母配对两次,它只是绕过它。它正在处理符号输入,但我不确定它为什么不在这里工作
以下是导入的文本文件:
code_file = open("words.txt", "r")
word_file = open("solved.txt", "r")
letter_file = open("letter.txt", "r")
以及:
这是Word文件的内容:
#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*
您的错误是一个简单的逻辑错误。当您确实需要一个
或
条件时,您有一个和
条件。将第二个while语句更改为:
while letter in words or len(letter) != 1
我已经将其添加到while循环中,但它仍然不起作用。这是不是意味着莱恩就在这里?@Kootch我想关键是把你的and改成or;不清楚,不确定你是否改变了。
while letter in words or len(letter) != 1