我想添加一個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
哦,我的天哪。我怎麼沒有注意到這一點。哈哈哈非常感謝你:) –