+ 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)

19th Jun 2020, 1:25 PM
Mr.Lightning
Mr.Lightning - avatar
7 ответов
+ 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)
19th Jun 2020, 4:09 PM
Ayush Kumar
Ayush Kumar - avatar
+ 3
return x + 1
19th Jun 2020, 1:28 PM
Gordon
Gordon - avatar
+ 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)
19th Jun 2020, 2:22 PM
Ayush Kumar
Ayush Kumar - avatar
+ 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
19th Jun 2020, 1:51 PM
Mr.Lightning
Mr.Lightning - avatar
+ 1
F҉R҉O҉N҉T҉ 🔚& 🔙🔚/ 𝔸𝕣𝕕𝕦𝕚𝕟𝕠 Lover I tried what you said but return y = 2 caused Invalid Syntax error
19th Jun 2020, 2:51 PM
Mr.Lightning
Mr.Lightning - avatar
19th Jun 2020, 4:14 PM
Mr.Lightning
Mr.Lightning - avatar
- 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
19th Jun 2020, 1:38 PM
Nboumakis