I'm happy to see you. I've recently (2 days ago to be precise) decided to pick python (python 3) as my first programming language. When I started playing around with functions I bumped into a problem I couldn't solve by myself and that's why I decided to ask for advice here.
I've created a simple function that counts from 0 to 100000 that is called when x = 0. The problematic part is that when it finished the function was supposed to return the new x value (which was 100001).
Here's the original code:
x = 0
def function(x):
while True:
print(x)
x += 1
if x == 100001:
return x
break
if x == 0:
function(x)
if x==100001:
print("TRUE")
else:
print("FALSE")
As I expected after compiling the program started counting at 0 and ended at 100000. Despite that it still printed out "FALSE" at the end. I played around with changing numbers and small details, but at the end it still didn't work as intended. Trying to pinpoint the error I reshaped my code into this:
x = 0
def function(x):
while True:
print(x)
x += 1
if x == 100001:
print("x= %d" % x)
return x
break
if x == 0:
function(x)
if x==100001:
print("TRUE")
else:
print("x= %d" % x)
After counting at the end of output I got the following lines: x = 100001 x = 0
I think that the something is wrong with my function returning "x" properly. I tried to find the answer myself, but failed. Any ideas?