Python3.x函数的值不正确
我不明白为什么我的代码给了我错误的值。。作为Python的初学者,我正在学习def 所以我的代码是:Python3.x函数的值不正确,python,function,Python,Function,我不明白为什么我的代码给了我错误的值。。作为Python的初学者,我正在学习def 所以我的代码是: 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_for
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(f"With a starting point of: {start_point}")
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))
当我运行我的程序时,我的答案是:
起点:10000
我们将有5000000颗豆子,5000.0罐和50.0箱
我们也可以这样做:
我们将有5000000颗豆子,5000.0罐和50.0箱
但我认为这应该是:
起点:10000
我们将有5000000颗豆子,5000.0罐和50.0箱
我们也可以这样做:
我们有50万颗豆子,500罐,5箱
像这样的事。。。因为起点=起点/10对吗
我做错了什么
Obs:是的,出于测试原因,我使用了不同的打印方法。您需要回忆一下秘方,如:
测试代码:
结果:
你需要回忆一下秘方,比如:
测试代码:
结果:
更改起始点的值并不意味着重新计算豆子、板条箱和罐子的值。您必须重新分配它们以获得正确的值
更改起始点的值后,尝试重复调用秘密公式的行。更改起始点的值并不意味着重新计算豆子、板条箱和罐子的值。您必须重新分配它们以获得正确的值
更改起始点的值后,请尝试重复调用秘密公式的行。在您第一次运行秘密公式时,豆子、罐子和板条箱的值已经指定。更改start_point将更新变量start_point的值,而不是其他变量的值 通过再次运行secret_公式,您将获得所需的新值
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(f"With a starting point of: {start_point}")
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)
print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))
豆子、罐子、板条箱的价值在您第一次运行secret_公式时已指定。更改start_point将更新变量start_point的值,而不是其他变量的值 通过再次运行secret_公式,您将获得所需的新值
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(f"With a starting point of: {start_point}")
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)
print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))
您忘记第二次调用secret\u公式更改start\u point的值不会影响其他变量!我想他没有忘记。我认为他更倾向于假设更改该公式的输入将自动更新结果,这显然没有发生。您忘记再次调用secret_公式更改start_point的值不会影响其他变量!我想他没有忘记。我认为他更倾向于假设将输入更改为该公式将自动更新结果,这显然不是那么简单!谢谢大家!!太简单了!谢谢大家!!
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can have also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
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(f"With a starting point of: {start_point}")
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)
print("We can have also do that this way:")
print("We'd have {} beans, {} jars, and {} crates.".format(beans, jars, crates))