【发布时间】:2021-06-08 02:29:00
【问题描述】:
#important variables
thePhrase = input("What phrase will we be dealing with today?")
menuInput = input("What would you like to do with the phrase?")
#search query tables
phraseLenArray = ["LEN", "LENGTH", "TEXT LENGTH", "PHRASE LENGTH", "STRING LENGTH", "LENGTH OF THE TEXT", "LENGTH OF THE PHRASE", "LENGTH OF THE STRING", "HOW LONG IS THE TEXT", "HOW LONG IS THE PHRASE", "HOW LONG IS THE STRING"]
phraseFilterArray = ["FILTER", "FILTER THE TEXT", "FILTER THE PHRASE", "FILTER THE STRING",]
if menuInput == phraseLenArray.lower():
phraseLen()
elif menuInput == phraseFilterArray.lower():
phraseFilter()
def phraseLen():
print(len(thePhrase))
def phraseFilter():
filterTxt = input("What are you trying to filter for?")
if filterTxt in thePhrase:
print("The filtered text,", filterTxt, "was found in the text.")
else:
print("The filtered text was not found in the text")
我试图让输入“menuInput”接受来自数组的答案,不区分大小写。 当我运行此代码时,错误消息是, "第 10 行:AttributeError: 'list' 对象没有属性 'lower'"
【问题讨论】:
-
您正在尝试更改列表的大小写。这将引发错误。您需要像
phraseLenArray=[str(x).lower() for x in phraseLenArray]这样遍历列表。只是好奇,为什么要将中的单词转换为小写?由于它有一个大写短语,您可以将输入转换为大写。
标签: python arrays string input