- 1

How can i make a global variable

How can i make a variable that will save its self in the python program example: def new_account(): user_name = input() password = int(input()) return user_name and password

5th Jul 2022, 8:26 PM
Bogyo - Leția Eduard
Bogyo - Leția Eduard - avatar
6 Respostas
+ 5
What do you mean with save itself? But in case you want a global variable, just do: global user_name user_name = input()
5th Jul 2022, 8:33 PM
Janne
Janne - avatar
+ 3
You can write two functions, each of them returning a single value. Or Python also allows to return several values as a tuple. Then you also need to destructure the result into multiple variables, or use indices. def multi_return(): return ('hello', 42) first, second = multi_return() Declaring global variables is bad practice, avoid if possible.
5th Jul 2022, 8:52 PM
Tibor Santa
Tibor Santa - avatar
+ 1
I want to make a global variable
5th Jul 2022, 8:54 PM
Bogyo - Leția Eduard
Bogyo - Leția Eduard - avatar
0
# Hi Bogyo - Leția Eduard # When you assign a value to a variable in the modul layer, it become global. # So if return a value from a function, and assign the returned value in a variable, it will of course be global. But if you want to assign a value to a global vaiable from inside the function, you have to use the keyword ’global’: >> def f(x): >> global y >> y = 2 * x >> >> f(10) # Creates the global variable y. >> print(y) # Print the global variable y. 20
5th Jul 2022, 10:45 PM
Per Bratthammar
Per Bratthammar - avatar
0
use global keyword to use or assign global variable
6th Jul 2022, 3:04 PM
N. Vimukthi Dilshan Fernando