Python Function Parameter As A Global Variable
I have written the following function, it takes in a variable input_name. The user then inputs some value which is assigned to input_name. I want to know the best way to make input
Solution 1:
The simplest thing would be to return the value, and assign it outside the function:
defmy_input(prompt):
#.. blah blah..return the_value
month = my_input("Please enter the month")
# etc.
Solution 2:
Other people are saying something like this:
definput(prompt):
return value
value = input(param1,param2, ...)
And that's what you really want to be doing, but just so you know, you can use globals() for changing global variables:
definput(input_name, prompt):
globals()[input_name] = value
Solution 3:
What you want to do is probably a bad practice. Just return input_name
from the input
function.
definput(param1,param2):
return value
value = input(param1,param2, ...)
Post a Comment for "Python Function Parameter As A Global Variable"