python - 如何检查输入的一部分 | Python
问题描述
我想检查输入的特定部分,但我不知道它在“哪里”
我试过这个:a = input('Enter command')
if a[0:5] == '(Command name)':
if a[7:?] == '(Subject)':
if a[?:len(a)] == '(Choice)':
To be continued
因此,输入分为三个部分;命令、主题以及它将运行的命令类型。但是应该使用哪个索引?是?我不知道这个词的长度。这是不可能的吗?
不,我知道我能做到,只是不知道如何做到。
解决方案
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)
推荐阅读
- python - numPy - 创建函数值数组
- docker - Docker 仅为当前会话添加 --insecure-registry
- javascript - 使用返回 [object Object] 作为表名的 JS 对象查询 MySQL
- javascript - 基本 Chrome 扩展的内容和后台脚本之间的连接
- reactjs - .Net Core 2.1 React 和 Signalr 1.0.4 抛出 /negotiate 404 错误
- r - tidyr:按另一个 data.frame 中的值对过滤
- image - 如何在 MATLAB GUI 中更改 UIAxis 中图像的区域?
- sql - Teradata SQL 3 变量根据条件分为两个变量
- excel - 在 SAP 脚本中,是否可以按名称而不是按行位置选择列?
- javascript - 可观察到数据列表在 Edge 中无法正常工作的淘汰赛