-1
def chek(name):
    for i in range(3):
        print(name)
        print(i)
        if i == 1:
            chek(name="name2")
        elif name == 'name2':
            print("changed name: ",name)
            return name

s = chek("name1")

print("final name:",s)

In this code my expected output is :

name1
0
name1
1
name2
0
changed name: name2
final name: name2

Actual output is :

name1
0
name1
1
name2
0
changed name:  name2
name1
2
final name:  None

Can anyone help me to understand, is anything wrong in my code or it's is behavior

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

1 Answers1

0

Try with updating this part:


if i == 1:
    return chek(name="name2")

this would return the second check() calling value;

Rafik Farhad
  • 1,182
  • 2
  • 12
  • 21
  • thanks, i got the flow `if i == 1: chek(name="name2")` this returns 'name2' which i was not return again to exit the loop and it resume the for loop where it got stop then it don't meet both the condition and hence return `None` thnks – sauravmalik Jun 02 '20 at 10:07