我想添加一个if语句来检查无效输入。如果用户输入“是”,并且如果用户输入“否”,则结束它的工作。但是出于某种奇怪的原因,不管答案是什么:是的,不,随机的字符等等。它总是打印“无效输入”语句。我试图仅在答案不是“是”或“否”时才打印。如果语句总是打印(Python)
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
哦,我的天哪。我怎么没有注意到这一点。哈哈哈非常感谢你:) –