简体   繁体   English

我需要使用数字组合功能的帮助

[英]I need help on combining functions using numbers

I'm new to programming and right now I'm learning about functions and everything related to them. 我是编程的新手,现在我正在学习函数以及与它们相关的所有知识。 I've been making some example of my own, and I ran into a problem. 我一直在做一些自己的例子,但遇到了一个问题。

 def test(x):
    number = x + 1
    return (number)
def test2(y):
    number2 = test(y) + 2
    print (number2)
test2(1)

If I print this function, it would work. 如果我打印此功能,它将起作用。 However I don't understand something. 但是我不明白。 Why is it that when I put number 1 into test2(y), it also goes into test(x)? 为什么当我将数字1放入test2(y)时,它也进入test(x)? Why does this happen? 为什么会这样?

You defined def test2(y): so when you run test2(1) then it creates local variable y inside test2 and it assigns y = 1 - and then it starts to execute commands inside test2 . 您定义了def test2(y):因此,当您运行test2(1)它将在test2内创建局部变量y并为其分配y = 1然后开始在test2内执行命令。

Inside test2 you execute test(y) so it gets value from y and executes test(1) . test2内部,您执行test(y)以便它从y获取值并执行test(1) And again: you defined def test(x) so it creates local variable x inside test , and it assigns x = 1 and then it starts to executes commands inside test . 再次:您定义了def test(x)以便在test内创建局部变量x ,并为其分配x = 1 ,然后开始在test内执行命令。 So now 1 is inside test 所以现在1是内部test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM