如果其关键字在特定范围中,则运行一个函数在列表中

发布于 2025-01-18 05:02:54 字数 411 浏览 2 评论 0原文

我有一个单词列表,当输入该单词时,它将运行特定的功能,如果列表中没有某个单词,那么它会打印出“无效!”

commLineInput = input("Input Command: ")



commandList = ["calculator","security"]

def commSlctandRun():
 if commLineInput == commandList in range (0,10) : 
    return commandList(commLineInput)
 else:
    print("Not valid!")

谁能解释一下我需要做什么,

它将比较输入和列表中前 11 个条目中的单词,如果不在列表中,它将打印为无效。结果是代码可以运行,但运行函数后会打印出“Not Valid!”

I had a list of word that when typed, would run a specific function and if a word was not in the list then it would print out "Not Valid!"

commLineInput = input("Input Command: ")



commandList = ["calculator","security"]

def commSlctandRun():
 if commLineInput == commandList in range (0,10) : 
    return commandList(commLineInput)
 else:
    print("Not valid!")

.Can anyone explain what i need to do

it would compare the input and the words in the list within the first 11 entries of the list and if not in the list it would print out as invalid . The result was that the code would run but after running the function it would print out "Not Valid!"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2025-01-25 05:02:54

我很难理解您要准确地做什么,但是这里有一些提示:

您可以轻松地知道element是否在àlistInside:

list_of_things = ["a", "b", "c", "d"]

element = "a"

if element in liste_of_things:
    print(f"{element} is in list_of_things")

如果您想限制列表的长度:

if element in liste_of_things[:10]:
    print(f"{element} is in the 10 first element of list_of_things")

希望这会有所帮助。

现在,关于您的代码:

commandList = ["calculator","security"]
...
    if commLineInput == commandList in range (0,10) : 
        return commandList(commLineInput)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^

命令清单不是函数,它是列表,您可以这样称呼它。即使使用Square Braket,它也无法使用,您也必须使用int来指定索引。您还可以从元素commandlist.index(CommlineInput)获取索引,该知道列表的第一个元素匹配commlineinput可能很有用。

另外:

if commLineInput == commandList in range(0,10): 

我知道Python将如何解释这一点...我的猜测,它将进行比较ET,结果在范围(0,10)中。比较将是truefalse,因此最终结果将为false,因为range(0,10)给予给予整数迭代器而不是布尔。

I have a bit of difficulty to understand what you want to do exactly, but here some cue:

You can easily know if an element is inside à list:

list_of_things = ["a", "b", "c", "d"]

element = "a"

if element in liste_of_things:
    print(f"{element} is in list_of_things")

If you want to limitate the length of the list:

if element in liste_of_things[:10]:
    print(f"{element} is in the 10 first element of list_of_things")

Hope that will help.

Now, about your code:

commandList = ["calculator","security"]
...
    if commLineInput == commandList in range (0,10) : 
        return commandList(commLineInput)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^

commandList is not a function, it's a list, you can call it like that. Even with square braket it won't work, you have to use int to specify the index. You can also get the index from an element commandList.index(commLineInput) which could be useful to know the position of the first element matching commLineInput of the list.

Also:

if commLineInput == commandList in range(0,10): 

I'm know sure how Python will interpret this... my guess, it will do the comparison et look it the result is in the range(0, 10). The comparison will be True or False so the final result will be False since range(0, 10) give en iterator of integer and not boolean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文