在函数中更改变量,但它返回原始值。

huangapple go评论67阅读模式
英文:

Changing variable in a function but it returns as the original value

问题

我在函数内改变了一个变量,但当它返回到原来的函数时,它会恢复到原来的状态。我的代码如下:

def myfunct1(v1, v2):
     v1 = v1 * v2
     print(v1)
     return v1

def myfunct0(v1, v2):
    myfunct1(v1, v2)
    print(v1)

v1 = int(input("输入一个数字:"))
v2 = int(input("输入第二个数字:"))

myfunct0(v1,v2)

在代码中有其他函数,但这部分给我带来了麻烦。如何使它返回的 v1 值与 myfunct1 中的值相同?我知道它进行了计算,因为它在 return v1 行之前打印了正确的数字。然而,当它从 myfunct0 中打印 v1 时,它会给出原始数字。有没有办法解决这个问题,而不必添加更多变量?

英文:

I am changing a variable within a function, but when it is returned to its original function, it reverts to the original. My code is

def myfunct1(v1, v2):
     v1 = v1 * v2
     print(v1)
     return v1

def myfunct0(v1, v2):
    myfunct1(v1, v2)
    print(v1)

v1 = int(input("Enter a number: "))
v2 = int(input("Enter a second number: "))

myfunct0(v1,v2)

There are other functions in the code, but this is the part that is giving me trouble. How can I get the v1 value it returns to be the same as it is in myfunct1? I know that it does the calculation because it prints the correct number before the return v1 line. However, when it prints v1 from myfunct0, it gives the original number. Any way to fix this without adding more variables?

答案1

得分: 1

你必须将返回值分配给你的v1变量。像这样:

def myfunct1(v1, v2):
    v1 = v1 * v2
    print(v1)
    return v1

def myfunct0(v1, v2):
    v1 = myfunct1(v1, v2)    # <--- 修改的行 ---
    print(v1)

v1 = int(input("输入一个数字:"))
v2 = int(input("输入第二个数字:"))

myfunct0(v1, v2)

调用一个函数不会改变变量的值。每个函数都有自己的上下文,所以在函数内部改变变量的值不会自动反映在函数外部。变量不是全局的。请查阅有关变量作用域的信息。

英文:

You have to assign the return value to your v1 variable. Like this:

def myfunct1(v1, v2):
     v1 = v1 * v2
     print(v1)
     return v1

def myfunct0(v1, v2):
    v1 = myfunct1(v1, v2)    # &lt;-- modified line ---
    print(v1)

v1 = int(input(&quot;Enter a number: &quot;))
v2 = int(input(&quot;Enter a second number: &quot;))

myfunct0(v1,v2)

Calling a function does not change the variable. Each function has its own context, so changing the value of a variable inside function is not automatically reflected outside said function. Variables are not global. Read up on variable scope.

答案2

得分: 1

只返回翻译好的部分:

你可以通过多种方式使其与 myfunct1 中的值相同(一种愚蠢的方式是将 v1 和 v2 都设置为1,因此它们永远不会改变)。

我假设你真正想问的是,“我如何在 myfunct0 内保存并使用 v1 的值,而 myfunct1 也要使用它?”

要做到这一点,有两种选项:

1.(最简单的方法是)只需在 myfunct0 内保存它:

def myfunct0(v1, v2):
    v1 = myfunct1(v1, v2) # 现在代码将两次打印相同的数字
    print(v1)

2.(更复杂的方式,如果你想使用可变的东西)是将v1定义为可变对象(如列表),然后进行更新:

def myfunct1(v1, v2):
    v1[0] = v1[0] * v2
    print(v1[0])
    return v1[0]

def myfunct0(v1, v2):
    myfunct1(v1, v2)
    print(v1[0])

v1 = [1]
v2 = 2
myfunct0(v1,v2)
# 2
# 2

注意:上述代码中的注释是英文原文,未进行翻译。

英文:

You can get it to have the same value as in myfunct1 many ways (one silly way is to just set both v1 and v2 to 1, therefore they never change)

I assume what you are really asking is, "how can I save and use value of v1 within myfunct1, within the broader function myfunct0?"

To do that, there are two options:

  1. (The simplest way is to) just save it within myfunct0:
def myfunct0(v1, v2):
    v1 = myfunct1(v1, v2) # code will now print the same number twice
    print(v1)
  1. (More convoluted way, if you want to use mutable things) is to define v1 as a mutable object (like a list), and then update that:
def myfunct1(v1, v2):
    v1[0] = v1[0] * v2
    print(v1[0])
    return v1[0]

def myfunct0(v1, v2):
    myfunct1(v1, v2)
    print(v1[0])



v1 = [1]
v2 = 2
myfunct0(v1,v2)
# 2
# 2

huangapple
  • 本文由 发表于 2023年6月26日 08:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定