我正在制作一个简单的命令行应用程序,其中一个命令打印您在“打印”一词后输入的文本。命令工作正常,我正在使用原始输入。但是,命令使用分割输入,从中提取文本就像使用表(string [num])。但是,在执行代码时,我收到错误消息:
>>>
:: print hello world
Traceback (most recent call last):
File "C:/Python34/Commands.py", line 10, in <module>
for i in range(1, len(splitcmd-1)):
TypeError: unsupported operand type(s) for -: 'list' and 'int'
>>>
我使用的代码是:
cmdlst = ['open', 'print', 'calculate']
files = ['Hello World', 'Hello Jeremy', 'Hello Dog']
while True:
cmd = input(':: ')
splitcmd = cmd.split()
if splitcmd[0] == 'open':
print(files[int(splitcmd[1])])
if splitcmd[0] == 'print':
for i in range(1, len(splitcmd-1)):
print(splitcmd[i], end = '')
print('', end='\n')
我的目标是避免在命令中使用引号(''/“”)。谢谢你的帮助!
答案 0 :(得分:3)
更改
for i in range(1, len(splitcmd-1)):
到
for i in range(1, len(splitcmd)-1):
括号不正确。
答案 1 :(得分:1)
您有以下错误
for i in range(1, len(splitcmd-1)):
你应该从长度减去1而不是从字符串:
for i in range(1, len(splitcmd)-1):
答案 2 :(得分:0)
我必须说你的代码不是pythonic,当你使用
时testConfig = {
reportTitle: 'Test Execution Report',
outputPath: reportPath,
seleniumServer: browser.seleniumAddress,
applicationUrl: browser.baseUrl,
testBrowser: browserName + ' ' + browserVersion
};
new HTMLReport().from(reportPath + '/junitresults.xml', testConfig);
}
这将导致另一个问题.......如果你的some_string_name不是一个数字。 int()将采用字符串编号,但不是字符串。