cmds = ['time']
while True:
inp = input('::> ')
sinp = inp.split()
if str(sinp[0]) in cmds:
print('mkay.')
如果名称和输入匹配,我能得到表中项目的位置吗?谢谢!你知道吗
更新:这是我的更新代码:
cmds = ['k', '1']
while True:
inp = input('>>> ')
sinp = inp.split()
try:
if str(sinp[0]) in cmds:
cmds.index(sinp)
print(sinp)
except ValueError:
print('Unknown Command')
每当我输入k或k时,它都会返回“未知命令”。1也是一样,但是“1”起作用。为什么会这样?你知道吗
哦,上帝。很抱歉打扰你们,我只是为.index做了sinp而不是sinp[0]。哎哟。你知道吗
更新:它不接受“1”或“1”。即使它在cmds表中。你知道吗
list的
index()
方法就是您所需要的。你知道吗确保将它放在
try
,except
块中,以防找不到该命令。你知道吗例如
您可以使用
you_list.index(the_item)
输出:
如果cmds是“表”,那么
cmds.index
会给出匹配字符串所在的位置。你知道吗相关问题 更多 >
编程相关推荐