目前,我正在尝试在 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()
?
最佳答案
print('Two pair, {0}s and {1}s'.format(*cards))
你只缺少星星 :D
关于python - 返回要馈送到 string.format() 的参数元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/539066/