简体   繁体   English

如果语句始终打印(Python)

[英]If statement always prints (Python)

I'm trying to add an if statement to check for invalid input. 我试图添加一个if语句来检查无效的输入。 It's working like it should and looping back again if the user enters "Yes" and ending if the user enters "No". 它工作正常,如果用户输入“是”,则再次循环返回;如果用户输入“否”,则结束循环。 But for some odd reason no matter what the answer: yes, no, random character, etc. It always prints the "invalid input" statement. 但是由于某种奇怪的原因,不管答案是什么:是,否,随机字符等。它始终打印“无效输入”语句。 I'm trying to make that only print when the answer isn't "Yes" or "No". 我正在尝试仅在答案不是“是”或“否”时打印。

while cont == "Yes":
    word=input("Please enter the word you would like to scan for. ") #Asks for word
    capitalized= word.capitalize()  
    lowercase= word.lower()
    accumulator = 0

    print ("\n")
    print ("\n")        #making it pretty
    print ("Searching...")

    fileScan= open(fileName, 'r')  #Opens file

    for line in fileScan.read().split():   #reads a line of the file and stores
        line=line.rstrip("\n")
        if line == capitalized or line == lowercase:
            accumulator += 1
    fileScan.close

    print ("The word", word, "is in the file", accumulator, "times.")

    cont = input ('Type "Yes" to check for another word or \
"No" to quit. ')  #deciding next step
    cont = cont.capitalize()

    if cont != "No" or cont != "Yes":
        print ("Invalid input!")

print ("Thanks for using How Many!")  #ending

That's because whatever you enter, at least one of the tests is True: 那是因为无论您输入什么,至少其中一项测试为True:

>>> cont = 'No'
>>> cont != "No" or cont != "Yes"
True
>>> (cont != "No", cont != "Yes")
(False, True)
>>> cont = 'Yes'
>>> cont != "No" or cont != "Yes"
True
>>> (cont != "No", cont != "Yes")
(True, False)

Use and instead: 使用and代替:

>>> cont != 'No' and cont != 'Yes'
False
>>> cont = 'Foo'
>>> cont != 'No' and cont != 'Yes'
True

or use a membership test ( in ): 或使用成员资格测试( in ):

>>> cont not in {'Yes', 'No'}  # test against a set of possible values
True

if cont != "No" or cont != "Yes":

Both Yes and No sastisfy this condition YesNo满足此条件

It should be cont != "No" and cont != "Yes" 应该是cont != "No" and cont != "Yes"

Cont will always either not equal "No" or not equal "Yes". 续将始终不等于“否”或不等于“是”。 You want and instead of or . 您需要and而不是or

Or, alternately, 或者,

if cont not in ['No', 'Yes']:

which will be more extensible if you want to, eg, add lowercase. 如果您想增加扩展性,例如添加小写字母。

if cont != "No" or cont != "Yes" means "if the answer is not No or it is not Yes". if cont != "No" or cont != "Yes"意思if cont != "No" or cont != "Yes"答案是否定的”。 Everything is either not No or not Yes, since it can't be both. 一切都不是否或不是,因为不可能两者兼而有之。

Change it instead to if cont not in ("Yes", "No") . 将其更改为if cont not in ("Yes", "No")

You need an and operation instead of or . 您需要使用and操作代替or Using or operation, your condition will evaluate to true no matter what the input value is. 使用操作,无论输入值是多少,您的条件都将评估为true。 Change the condition to: 将条件更改为:

if cont != "No" and cont != "Yes":

or simply use: 或简单地使用:

if cont not in ("No", "Yes"):
if cont != "No" or cont != "Yes":

您可能输入什么不满足此要求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM