Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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_String_Printing_Tuples_Formatted - Fatal编程技术网

打印格式字符串函数和变量(python)

打印格式字符串函数和变量(python),python,string,printing,tuples,formatted,Python,String,Printing,Tuples,Formatted,函数和变量的定义: def secret_formula(started): jelly_beans = started * 500 jars = jelly_beans / 1000 crates = jars / 100 return jelly_beans, jars, crates start_point = 10000 / 10 打印报表: print """crazy different style: startpoint: %d\n beans:

函数和变量的定义:

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

start_point = 10000 / 10
打印报表:

print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % (start_point, (secret_formula(start_point)))
我收到的错误消息是“%d”格式:需要数字,而不是元组。
请帮我修复它。我对编程真的很陌生……或者只是不可能将变量和调用的函数打包到同一个formattet print中?

python 2中的变量:

print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % ((start_point, ) + secret_formula(start_point))
在这里,我通过将tuple
(起点)
添加到函数的结果中来创建一个新的tuple


在python 3中,您可以:


考虑添加
*
,以解压缩元组(python 3.x解决方案):

如果您使用的是python 2.x,则可以键入:

start_point = 10000 / 10
res = secret_formula(start_point)
print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % (start_point, res[0], res[1], res[2])
在Python3上工作

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

start_point = 10000 / 10

print ("startpoint : {0} \n \
       beans:\t\t {1}  \n \
       jar :\t\t {2}".format(start_point,*secret_formula(start_point)[:2]))
输出

startpoint : 1000.0 
    beans:       500000.0  
    jar :        500.0

您的问题陈述中就有解决方案。
%d格式:需要数字,而不是元组。
。另请参阅关于python
字符串格式。
。如果您是编程新手,请学习基本知识。任何关于python的像样的教程/站点都会有一些关于python基本构造的东西,你想了解。你似乎想把元组
(secret\u formula(start\u point))
解压成几个参数。您可以与拆包操作员一起执行此操作;i、 e.
*(秘密公式(起点))
#According to zen of python Explicit is better than implicit, so

bs, js, cs = secret_formula(start_point)

print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % (start_point, bs, js, cs)
def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

start_point = 10000 / 10

print ("startpoint : {0} \n \
       beans:\t\t {1}  \n \
       jar :\t\t {2}".format(start_point,*secret_formula(start_point)[:2]))
startpoint : 1000.0 
    beans:       500000.0  
    jar :        500.0