- 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
6 ответов
+ 5
What do you mean with save itself? But in case you want a global variable, just do:
global user_name
user_name = input()
+ 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.
+ 1
I want to make a global variable
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
0
use global keyword to use or assign global variable