无法将变量与while循环中的输入进行比较

时间:2014-06-02 13:15:30

标签: 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, 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")

solved = word_file.read()
words = code_file.read()
clue = clues_file.read()

这是单词file:

的内容
#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*

1 个答案:

答案 0 :(得分:1)

您的错误是一个简单的逻辑错误。当你真的需要and条件时,你有一个or条件。将您的第二个while语句更改为:

while letter in words or len(letter) != 1