我正在用Python编写一个简单的程序来确定一个单词是否包含在一段文本中。问题在于,当在文本片段中检测到desired_word时,期望_word_found变量未被语句" desired_word_found == true"更改。我知道这与变量(desired_word_found)不是静态有关,我甚至试图将变量更改为全局,但它仍然没有被该语句改变。我是Python新手,需要一些帮助。
#这个程序确定给定的单词是否在一段文字中
desired_word_found = False
#retrieving necessary data
desired_word = input("What word would you like to search for? ")
raw_text = input("Please enter the text you would like us to search: ")
words = raw_text.split()#stores each word of the text in words list
for i in range(len(words)):#loops through list of words to determine if our desired word is contained in the list
if words[i] == desired_word:
desired_word_found == True ################### VARIABLE IS NOT BEING CHANGED BY THIS STATEMENT
if desired_word_found == True:
print(desired_word+" was found")
else:
print(desired_word+" was not found")
答案 0 :(得分:2)
为变量赋值由ONE等于标记:
desired_word_found = True