这就是我要做的事情:
Time Distance
---------- ----------
1 100
2 200
我试过了:
count = 1
print(' Hour ' + '\t' + ' Distance ')
print('----------' + '\t' + '----------')
while count <= timeTraveled:
print(str.center(10[str(count)]) + '\t' + str.center(10[str((speedOfVehicle * count))]))
count = count + 1
无论我为了格式化目的而尝试将变量设置为字符串,我总是会遇到:
TypeError: 'int' object is not subscriptable
答案 0 :(得分:0)
我认为我发现了你的问题:你错误地将documentation的元语法误认为是Python语法。
当文档描述带有可选字段的命令时,括号表示选项部分。这种描述风格至少有45年的历史,可以追溯到IBM驱动西半球所有计算机的日子。有关编写命令的帮助,请参阅更远的示例。
在您的情况下,您需要类似
的内容print(str(count).center(10))
这是字符串类的方法;它在您调用它的字符串上运行。在您的情况下,该字符串是str(count)
。你可以从那里跟随其他人吗?