如果其关键字在特定范围中,则运行一个函数在列表中
我有一个单词列表,当输入该单词时,它将运行特定的功能,如果列表中没有某个单词,那么它会打印出“无效!”
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很难理解您要准确地做什么,但是这里有一些提示:
您可以轻松地知道
element
是否在àlistInside:如果您想限制列表的长度:
希望这会有所帮助。
现在,关于您的代码:
命令清单
不是函数,它是列表,您可以这样称呼它。即使使用Square Braket,它也无法使用,您也必须使用int
来指定索引。您还可以从元素commandlist.index(CommlineInput)
获取索引,该知道列表的第一个元素匹配commlineinput
可能很有用。另外:
我知道Python将如何解释这一点...我的猜测,它将进行比较ET,结果在
范围(0,10)
中。比较将是true
或false
,因此最终结果将为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:If you want to limitate the length of the list:
Hope that will help.
Now, about your code:
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 useint
to specify the index. You can also get the index from an elementcommandList.index(commLineInput)
which could be useful to know the position of the first element matchingcommLineInput
of the list.Also:
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 beTrue
orFalse
so the final result will beFalse
sincerange(0, 10)
give en iterator of integer and not boolean.