Code:
n = 10
def test():
global n
while n > 0:
n -= 1
print(n)
if n == 2:
test()
print("aaa")
test()
Output:
9
8
7
6
5
4
3
2
1
0
aaa
aaa
Why does it write "aaa" twice?
I need to use it in my another project but it writes it twice.
When you call test() in the if statement, that causes the function to print out the rest of the numbers and then "aaa".
Once it has done that, the program returns to the point at which test() was last called (in the if statement) so it carries on running and prints "aaa" again.