definition of function & variable:
函数和变量的定义:
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 statement:
print语句:
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)))
The error message I get is " %d format: a number is required, not a tuple. Please help me fix it. I´m really new to programming...or is it just not possible to pack a variable and a called function into the same formattet print?
我得到的错误消息是“%d格式:一个数字是必需的,而不是一个元组。请帮我修理一下。我真的´m新的编程…还是不可能将一个变量和一个被调用的函数打包到相同的formattet print中?
4 个解决方案
#1
0
#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)
#2
0
Consider adding *
in order to unpack the tuple (python 3.x solution):
考虑添加*以打开tuple (python 3)。x解决方案):
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))))
If you're using python 2.x can type:
如果你使用的是python2。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])
#3
0
Working on python 3
在python 3
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]))
output
输出
startpoint : 1000.0
beans: 500000.0
jar : 500.0
#4
0
a variant for in python 2:
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))
where i create a new tuple by adding the tuple (start_point, )
to the result of your function.
在这里,通过将tuple (start_point)添加到函数的结果中,创建一个新的tuple。
in python 3 you can unpack the tuple with a *
:
在python 3中,您可以使用*:
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)))
#1
0
#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)
#2
0
Consider adding *
in order to unpack the tuple (python 3.x solution):
考虑添加*以打开tuple (python 3)。x解决方案):
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))))
If you're using python 2.x can type:
如果你使用的是python2。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])
#3
0
Working on python 3
在python 3
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]))
output
输出
startpoint : 1000.0
beans: 500000.0
jar : 500.0
#4
0
a variant for in python 2:
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))
where i create a new tuple by adding the tuple (start_point, )
to the result of your function.
在这里,通过将tuple (start_point)添加到函数的结果中,创建一个新的tuple。
in python 3 you can unpack the tuple with a *
:
在python 3中,您可以使用*:
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)))