【发布时间】:2018-07-12 01:15:27
【问题描述】:
这就是我想要做的:
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
【问题讨论】:
-
你期待什么,例如
10[str(count)]要做什么? -
@jonrsharpe,10 是空间量。 count 是整数变量数量。我希望将 count 打印为字符串并在指定的 10 个空格内居中。
-
@GOD 请发布完整代码。您如何获得 speedOfVehicle 它在哪里定义;以便社区可以帮助您编写python代码。
-
但是 1. 这不是任何 Python 字符串格式的正确语法; 2. 这实际上也不在字符串模板中,您正试图将其作为代码运行。也许read the docs?