+ 1
How to use return x = 1
I want to set the output of a function be x += 1 So I tried to do return x = x + 1 and it didn't work How can I write something instead of this? (x is not a local var)
7 Respostas
+ 1
O yes Mr.Lightning sorry this is correct one. I hadn't noticed that before
def test(x):
if x > 0:
y = 2
return y
else:
y = 4
return y
y=test(5)
print(y)
+ 3
return x + 1
+ 3
Hay Mr.Lightning bro you declare a y variable as a global ok . Dont think that the y will be overwritten by new value because the scopes are different one is local and one is global which is outside the function.
Now you can fix it by following code.
def test(x):
if x > 0:
return y = 2
else:
return y = 4
y=test(5)
print(y)
+ 1
The point is that I want to do something like this:
y = 3
def test(x):
if x > 0:
return y = 2
else:
return y = 4
test(5)
print(y)
Things that you said is when y is a local parameter.
But in this code I want this result:
Output:
2
+ 1
FÒRÒOÒNÒTÒ đ& đđ/ đžđŁđđŠđđđ Lover I tried what you said but return y = 2 caused Invalid Syntax error
+ 1
FÒRÒOÒNÒTÒ đ& đđ/ đžđŁđđŠđđđ Lover Thanks
My problem is solved right now.
- 1
A side-note on Gordon 's answer, while x+1 will be returned, "x" won't be changed. If you want to change "x" do:
x += 1
return x