![](/img/trans.png)
[英]If and elif statement are being ignored. Goes straight to the else statement Python
[英]If statement conditions being ignored and going straight to else no matter what you input
我對 Python 有點陌生,我正在為一個項目創建一個測試程序,這是一個你必須猜測某些 Python 短語的定義的游戲。 前半部分工作正常,后半部分有問題。 它應該從元組中隨機選擇一個定義,並要求您輸入正確的單詞。 但是,無論它是什么詞,它總是會轉到您輸入錯誤答案的 else 語句。 我嘗試了很多東西,改變了括號、變量以及介於兩者之間的所有內容。 這是有問題的后半部分:(變量在頂部定義,不包括在內)
while (len(wordPool) >= 1):
answer = input(random.choice(question) + " What word is that? ")
if question == vocabularyDefinitions[0] and answer == "List":
print("You got it right! That is a list!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[1] and answer == "Tuple":
print("That is correct! That is a tuple!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[2] and answer == "Datetime":
print("Nice job! That is the datetime module!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[3] and answer == "Random":
print("That's right! That is in fact the random module!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[4] and answer == "Include":
print("Yep, that's the include function! Well done!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[5] and answer == "Parameter":
print("That is a parameter, keep it up!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[6] and answer == "Index":
print("You must know your stuff! That's an index alright.")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[7] and answer == "Len":
print("This congratulatory message says well done!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[8] and answer == "Append":
print("You have a knack at this don't you? Keep going!")
score +=1
totalAttempts += 1
elif question == vocabularyDefinitions[9] and answer == "Insert":
print("Yes! You answered correctly!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[10] and answer == "Remove":
print("I definitely won't remove you after that correct answer! Which was remove by the way.")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[11] and answer == "Iterate":
print("That was in fact the definition of iterate in Python!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[12] and answer == "Loop":
print("It's highly unlikely the while loop will show you this message again, but if it does, consider yourself lucky!")
score += 1
totalAttempts += 1
elif question == vocabularyDefinitions[13] and answer == "Element":
print("The index number of this word is the same as the atomic number for Aluminum. The more you know!")
score += 1
totalAttempts += 1
elif (answer == "End"):
break
else:
print("Whoops! That wasn't the right answer!")
wrongAnswer += 1
print(str.format("The game is over, and got {} questions right, {} questions wrong, and your total attempts was {}.",score,wrongAnswer,totalAttempts))
我可以添加/刪除什么,以便在您的輸入中不會忽略 if/elif 語句?
據我所知,由於該變量未包含在帖子中,因此“問題”是一個可迭代的。 (所以為了簡單起見,假設問題是一個列表)
因此你做了...
answer = input(random.choice(question) + " What word is that? ")
為什么要比較列表和字符串?
question == vocabularyDefinitions[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.