我在python中编写一个简单的程序,该程序确定文字中是否包含一个单词。问题在于,当在文本中检测到Desired_word时,desired_word_found变量不会通过语句" desired_word_found == true"更改。我知道这与变量(desired_word_found(没有静态有关,我什至试图将变量更改为全局,但该语句仍然没有更改。我是Python的新手,需要在此方面提供一些帮助。
#this程序确定给定单词是否在文本中
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")
将一个值分配给变量是由一个等于标记完成的:
desired_word_found = True