How to store a user define value asked in a function in a variable which is outside of the function ?? | Sololearn: Learn to code for FREE!
0

How to store a user define value asked in a function in a variable which is outside of the function ??

28th Sep 2020, 5:45 AM
Chris Evans
Chris Evans - avatar
6 odpowiedzi
+ 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)
28th Sep 2020, 6:44 AM
Ipang
+ 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 :)
28th Sep 2020, 6:40 AM
G B
G B - avatar
+ 1
Ipang Yyess yess, I am with you, You figured it out well. Thanks for this, now it is clear to me.😍😍 Actually I am working with GUI in python and wanted to work with the fixed value which user gonna give 😎😎
28th Sep 2020, 7:25 AM
Chris Evans
Chris Evans - avatar
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 !😌😌
28th Sep 2020, 7:28 AM
Chris Evans
Chris Evans - avatar
0
Chris Evans The prints are just to Show what is happening ;)
28th Sep 2020, 7:57 AM
G B
G B - avatar
0
G B Yes ☺☺🙂 Thank you !
28th Sep 2020, 1:46 PM
Chris Evans
Chris Evans - avatar