Python 为什么在这段代码中同时打印变量和函数调用会产生错误?

Python 为什么在这段代码中同时打印变量和函数调用会产生错误?,python,Python,我已经通过Zed Shaw的“艰苦学习Python”开始学习Python 在中,我更改了下面的代码段: def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates start_point = 10000 beans, jars, crates = se

我已经通过Zed Shaw的“艰苦学习Python”开始学习Python

在中,我更改了下面的代码段:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)
为此:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
print "With a starting point of: %d we'd have %d, %d, %d" % (start_point,secret_formula(start_point))
为什么会出现错误:

TypeError: float argument required, not tuple
这条线什么时候行

print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

问题是
secret\u formula()
返回一个三元组:

(jelly_beans, jars, crates)
因此,原始打印声明:

print "With a starting point of: %d we'd have %d, %d, %d" % (start_point,secret_formula(start_point))
变成:

print "With a starting point of: %d we'd have %d, %d, %d" % (start_point, (jelly_beans, jars, crates))
您得到的特定
TypeError
是因为元组
(果冻豆、罐子、板条箱)
是第二个参数,其格式必须为
%d
,表示为十进制整数。元组不能隐式转换为int或float。我不完全清楚为什么它说“需要浮点参数”,而不是整数参数

还要注意,print语句包含4个
%d
s,因此它需要4个单独的值。你给了它2:

  • 起点

  • (果冻豆、罐子、板条箱)


  • 第二个print语句工作正常的原因是
    %
    运算符接受一个值,或者多个值打包到一个元组中,
    secret\u formula()
    恰好返回。

    因为
    secret\u formula
    的返回值是一个元组,当您写入:

    print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
    
    它变成

    print "We'd have %d beans, %d jars, and %d crates." % (1, 2, 3)
    
    要使其在示例中正常工作,您有两个选项:

  • 使用新的,带有:

  • start\u point
    放入一个元组,然后将其与函数调用的结果一起添加:

    print "With a starting point of: %d we'd have %d, %d, %d" % ((start_point,)+secret_formula(start_point))
    

  • 为了简单易读,我选择选项1,不幸的是,Burhan Khalid建议的
    *secret_公式(起点)
    构造在参数列表之外不起作用。但我们可以使用元组连接:

    def secret_formula(started):
        jelly_beans = started * 500
        jars = jelly_beans / 1000
        crates = jars / 100
        return jelly_beans, jars, crates
    
    
    start_point = 10000
    
    print "With a starting point of: %d we'd have %d, %d, %d" % ((start_point,) + secret_formula(start_point)) 
    
    输出

    With a starting point of: 10000 we'd have 5000000, 5000, 50
    

    (起点,)
    创建一个单元素元组,因此我们可以将其添加到
    secret\u formula()
    返回的元组中。我们需要外圆括号,因为
    %
    的优先级高于
    +

    谢谢您的建议,但是它给了我一个语法错误(无效语法)。知道为什么吗?我使用的是Python2.7.8不幸的是,
    *secret\u公式(起点)
    在参数列表之外不起作用(至少在Python2.6.4上不起作用)。但是我们可以在这里使用元组连接:
    ((起点,)+secret\u公式(起点))
    。我们需要外括号,因为
    %
    的优先级高于
    +
    。是的,选项1看起来不错;我们可以在这里使用星型解包结构,因为它在
    format()
    的参数列表中。更新适当注意的@BurhanKhalid。我们也可以使用像{:f}这样的选项而不是简单的{}来提高除法的精度
    With a starting point of: 10000 we'd have 5000000, 5000, 50