Python: I don't think I understand this expression I would appreciate it if you could pin down which part was wrong.

Asked 2 years ago, Updated 2 years ago, 21 views

def func(x=2):
        if x<=0:
           return
        print(x)
for x in range(3):
  func()
2
2
2

When I did it, this value came out, but I thought I was misunderstanding something, so I wanted to hear the answers from masters, so I posted a question. As I understand it,

Put 2 in the function Since 2 is greater than 0, print without return. Since 2 is substituted for x, 2 is output as shown in the above equation.

python

2022-09-22 18:27

1 Answers

To give you a simple answer, since the input parameter was not explicitly substituted for the function, the default value of 2 was substituted and executed.

In order to solve the problem, the variables must be explicitly substituted as shown below.

for x in range(3):
    func(x)


2022-09-22 18:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.