【问题标题】:Set a string input to accept an answer from an array of acceptable answers (Python 3.8.4)设置字符串输入以接受来自可接受答案数组的答案(Python 3.8.4)
【发布时间】: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


【解决方案1】:

phraseLenArray 是一个列表。当您尝试将列表转换为大写时,会引发错误。我对您的代码做了一些更改。

首先,始终在调用函数之前定义它们。在这里,您正在遵循程序编程,python 正在从上到下阅读。当遇到在调用您的函数后定义的函数时,它会引发错误。

其次,您可以使用.upper()将输入转换为大写字母。

这里是代码;

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",]
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")

if menuInput.upper() in phraseLenArray:
    phraseLen()
elif menuInput.upper() in phraseFilterArray:
    phraseFilter()


【讨论】:

  • 我试图让它接受列表中的答案,不管它是否大写。
  • @Koboman,这会增加区分大小写
  • 非常感谢您的帮助,如果您不介意的话。您能否解释一下 .upper 条件如何/为什么使文本不区分大小写而 .lower 不区分大小写?
  • 不,.upper() 也删除区分大小写。它将单词转换为大写。你说你试图让它接受列表中的答案,不管它是否大写。问题是python区分大小写,它会检查大小写是否匹配。 @Koboman
  • 我认为您的解决方案效果很好。谢谢!为了将来参考,我将如何将接受输入列表中的任何输入不区分大小写,例如 Accepted: yes, ya, yea, yup Input: yEs, yAm YEam YUp(如何接受这些潜在输入) Tl;博士,我如何才能到达 Python 从列表中接受答案的地方,只有在输入完全相同的字母时才接受它们,无论它们是大写还是小写
【解决方案2】:

如错误所述,phraseLenArray 是一个列表,因此没有 lower 方法。您可以尝试大写 menuInput:

if menuInput.upper() in phraseLenArray:

或使用地图和 lambda:

if menuInput in map(lambda s: s.lower(), phraseLenArray):

【讨论】:

    【解决方案3】:

    试试这个:

    for phraseLen in phraseLenArray:
        if phraseLen == menuInput:
            phraseLen()
    

    函数lower()string 的属性而不是list,因此您需要使用for 循环(或其他方式)来获取list 中的项目并将它们与您的@ 进行比较987654326@.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多