【问题标题】:Python 3.x function incorrect valuePython 3.x 函数值不正确
【发布时间】:2018-10-22 22:36:40
【问题描述】:

我不明白为什么我的代码给了我错误的值。作为 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_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 个板条箱。

我们也可以这样做:

我们有 500000 个豆子、500 个罐子和 5 个板条箱。

类似的东西...因为start_point = start_point / 10对吗?

我做错了什么?

Obs:是的,出于测试原因,我使用了不同的方法来“打印”。

【问题讨论】:

  • 你忘了第二次调用 secret_formula()
  • 改变start_point的值不会影响其他变量!
  • 我想他没有忘记。我认为他宁愿假设将输入更改为该公式会自动更新结果,这显然不会发生

标签: python function


【解决方案1】:

你需要记得secret_formula()like:

start_point = start_point / 10
beans, jars, crates = secret_formula(start_point)

测试代码:

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))

结果:

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.

【讨论】:

  • 如此简单!谢谢大家!
【解决方案2】:

改变starting_point的值并不意味着beans、crates和jars的值被重新计算。您必须再次重新分配它们以获得正确的值。

在更改starting_point的值后,尝试重复调用秘密公式的那一行。

【讨论】:

    【解决方案3】:

    在您第一次运行 secret_formula() 时,豆子、罐子、板条箱的值已经被分配。更改 start_point 将更新变量“start_point”的值,而不是其他值。

    通过再次运行 secret_formula(),您将获得您正在寻找的新值。

    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))
    

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 1970-01-01
      • 2016-12-06
      • 2020-01-28
      • 1970-01-01
      • 2014-04-13
      • 2020-11-13
      • 1970-01-01
      • 2016-01-01
      相关资源
      最近更新 更多