0
How to store a user define value asked in a function in a variable which is outside of the function ??
6 Antworten
+ 2
Chris Evans
So a function asks for user input and then pass the given input to a variable outside the function? if that's the case, then you can set the function to return the input, and assign the function's return value to the variable outside the function.
If that's not the case, then an edit is in order to better describe the goal.
def fun():
ud_data = input('Enter something: ') # user defined value
return ud_data
var = fun() # store in variable
print(var)
+ 1
I think you are searching for global variables.
To use a variable globally, it must be defined outside the function, and for Sure, before the function is called ..
To use a global variable inside a function, you must declare the variable as global inside the function.
This is, bis it works:
####################### Code start
myGlobalVariable = 'global'
def foo ():
global myGlobalVariable
myGlobalVariable *= 2
print(myGlobalVariable) #globalglobal
print(myGlobalVariable) #global
foo()
print(myGlobalVariable) #globalglobal
####################### Code end
The output would be:
global
globalglobal
globalglobal
Hope This helped :)
0
G B
No bro, not just print, even not print....
Store Directly to my variable in run time !(any variable)
Well Thank you for your support,
I appreciate it !😌😌
0
Chris Evans The prints are just to Show what is happening ;)
0
G B
Yes ☺☺🙂
Thank you !