Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 使用函数时的字符串格式问题_Python 2.7 - Fatal编程技术网

Python 2.7 使用函数时的字符串格式问题

Python 2.7 使用函数时的字符串格式问题,python-2.7,Python 2.7,我有一个我认为是令人尴尬的简单问题,但三个小时的谷歌搜索和检查stackoverflow并没有帮助 假设我有一段非常简单的代码: def secret_formula(started): jelly_beans = started*500 jars = jelly_beans/1000 crates = jars/100 return jelly_beans,jars,crates start_point = 10000 print("We'd have {} beans,

我有一个我认为是令人尴尬的简单问题,但三个小时的谷歌搜索和检查stackoverflow并没有帮助

假设我有一段非常简单的代码:

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

start_point = 10000

print("We'd have {} beans, {} jars, and {} crates.".format(secret_formula(start_point)))
发生的是我得到了“索引器:元组索引超出范围”。所以我只是打印
secret\u formula
函数,看看它是什么样子,它看起来像这样:

(5000000, 5000.0, 50.0)
基本上,它将输出视为一个“东西”(我还是一个新手,如果我的语言不正确,很抱歉)。我的问题是,为什么它会这样对待它,我如何让它通过三个输出(
jelly_beans
jars
,和
crates
),从而正确格式化字符串


谢谢

字符串的
format
函数采用可变数量的参数。
secret\u formula
函数正在返回一个元组。您希望将其转换为参数列表。这是使用以下语法完成的:

print("We'd have {} beans, {} jars, and {} crates.".format(*secret_formula(start_point)))

重要的PAR是<代码> */COD>字符。它告诉您要将以下iterable转换为参数列表以传递给函数。

谢谢!我试着用*但是我放错地方了,所以我放弃了。