首页 > 解决方案 > 如何检查输入的一部分 | Python

问题描述

我想检查输入的特定部分,但我不知道它在“哪里”

我试过这个:a = input('Enter command')

if a[0:5] == '(Command name)':

    if a[7:?] == '(Subject)':

        if a[?:len(a)] == '(Choice)':

            To be continued

因此,输入分为三个部分;命令、主题以及它将运行的命令类型。但是应该使用哪个索引?是?我不知道这个词的长度。这是不可能的吗?

,我知道我能做到,只是不知道如何做到。

标签: pythonlistinputindexing

解决方案


I suppose your input should go something like this:

command subject choice

So there are basically three parts(might be more). I suggest you break the input in list as:

a = [str(x) for x in input().split()]

To get the length of each entities:

len(entity)

And for index you can use index():

list.index(entity)

推荐阅读