【发布时间】:2009-02-11 22:10:10
【问题描述】:
目前,我正在尝试让 Python 中的方法返回一个包含零个、一个或两个字符串的列表以插入字符串格式化程序,然后将它们传递给字符串方法。我的代码如下所示:
class PairEvaluator(HandEvaluator):
def returnArbitrary(self):
return ('ace', 'king')
pe = PairEvaluator()
cards = pe.returnArbitrary()
print('Two pair, {0}s and {1}s'.format(cards))
当我尝试运行此代码时,编译器会给出 IndexError: tuple index out of range。
我应该如何构造我的返回值以将其作为参数传递给.format()?
【问题讨论】:
标签: python string-formatting tuples