在python中打印数组时这个错误感到困惑?

时间:2014-04-17 10:56:03

标签: python arrays console

考虑以下代码;我想从控制台屏幕的开头打印数组Ptimes 3空格。我试过了

print "   %s" %(Ptimes) 

当我使用此表单时,没有任何打印表明存在错误"由于字符串格式化而导致所有争论都被覆盖。

原始工作代码是:

PN = input("   Enter each process time following by its arrival time separated by comma: ")

Ptimes = PN[::2]  
Atimes = PN[1::2]
print    Ptimes
print    Atimes 

1 个答案:

答案 0 :(得分:1)

Ptimes是一个元组,Python希望在 Ptimes中找到每个*元素的占位符。将其包装在元组中(添加逗号):

print "   %s" % (Ptimes,)

演示:

>>> Ptimes = ('foo', 'bar')
>>> print "   %s" % (Ptimes)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>> print "   %s" % (Ptimes,)
   ('foo', 'bar')