Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,使用元组格式打印,不工作_Python_Python 2.7_String Formatting - Fatal编程技术网

Python,使用元组格式打印,不工作

Python,使用元组格式打印,不工作,python,python-2.7,string-formatting,Python,Python 2.7,String Formatting,代码如下: #! /usr/bin/python def goodDifference(total, partial, your_points, his_points): while (total - partial >= your_points - his_points): partial = partial+1 your_points = your_points+1 return (partial, your_points,

代码如下:

#! /usr/bin/python

def goodDifference(total, partial, your_points, his_points):

    while (total - partial >= your_points - his_points):
        partial = partial+1
        your_points = your_points+1

        return (partial, your_points, his_points)

def main():
     total = int(raw_input('Enter the total\n'))
     partial = int(raw_input('Enter the partial\n'))
     your_points = int(raw_input('Enter your points\n'))
     his_points = int(raw_input('Enter his points\n'))
     #print 'Partial {}, yours points to insert {}, points of the other player {}'.format(goodDifference(total, partial, your_points, his_points))
     #print '{} {} {}'.format(goodDifference(total, partial, your_points, his_points))
     print goodDifference(total, partial, your_points, his_points)

if __name__ == "__main__":
     main()
这两个带注释的打印格式不起作用,执行时会报告此错误:
indexer错误:元组索引超出范围
。 最后一次打印(未注释)效果良好。 我读过很多Python格式字符串的例子,我不明白为什么我的代码不起作用

我的python版本是2.7.6

str.format()
需要单独的参数,您将元组作为单个参数传递。因此,它将元组替换为第一个
{}
,然后就没有更多的项留给下一个了。要将元组作为单个参数传递,它:

str.format()
需要单独的参数,并且您要将元组作为单个参数传递。因此,它将元组替换为第一个
{}
,然后就没有更多的项留给下一个了。要将元组作为单个参数传递,它:


为什么不直接打印元组中的值呢

t = goodDifference(total, partial, your_points, his_points)
print '{', t[0], '} {', t[1], '} {', t[2], '}'

为什么不直接打印元组中的值呢

t = goodDifference(total, partial, your_points, his_points)
print '{', t[0], '} {', t[1], '} {', t[2], '}'

在这种情况下,*在做什么?将元组解压为多个参数。现在完美了。。。这个答案很有用*在这种情况下做什么?将元组解压成多个参数。现在完美了。。。这个答案很有用