6

I have written the following function:

  1. it takes in a variable input_name;
  2. The user then inputs some value;
  3. the value is assigned to input_name.

I want to know: the best way to make input_name accessible outside of the function.

I know that defining a variable as global, inside a function, means that it can be used outside of the function. However, in this case the variable acts actually as a parameter of the function so I am not sure how to define it as a global variable.

I appreciate any help with this, please find the code in question below:

def input(input_name, prompt):
    while True:
        data = raw_input(prompt)
        if data:
            try:
                input_name = int(data)
            except ValueError:
                print 'Invalid input...'
            else:
                if input_name >= 0 and input_name < 100:
                    print 'Congratulations'
                    break
                input_name = 'Please try again: '
        else:
            print 'Goodbye!'
            break

month = 0
day = 0
year = 0
century = 0

input(month, "Please enter the month (from 1-12, where March is 1 and February is 12): ")
input(day, "Please enter the day (from 1-31): ")
input(year, "Please enter the year (from 0 - 99, eg. 88 in 1988): ")
input(century, "Please enter the century (from 0 - 99, eg. 19 in 1988): ")

A = month
B = day
C = year
D = century
1
  • 1
    On a side note: input_name = 'Please try again: ' – You probably want to set prompt there, not input_name. Commented Feb 11, 2012 at 18:45

3 Answers 3

15

The simplest thing would be to return the value, and assign it outside the function:

def my_input(prompt):
    #.. blah blah..
    return the_value

month = my_input("Please enter the month")
# etc.
Sign up to request clarification or add additional context in comments.

4 Comments

+1 The way you get information out of a function is via the return value. I don't understand why so many people are so resistant to such a simple concept.
@KarlKnechtel It's not that they're resistant to it. I've found that many Python beginners don't really understand the return statement. Some don't see it as any different from print, because the REPL prints return values by default. See this discussion here, posted by Guido van Rossum: plus.google.com/115212051037621986145/posts/NJnmxZkrE4J I think they see functions more as simple code blocks or labels, and not procedures that return something when given input. That basic programming concept is not taught in most Python tutorials.
Thanks for the help here. So if I do return input_name inside the function then I should be able to say print input_name outside of the function? At present the program is just returning 0 (as I initialized the variables as 0).
Variables are names for values. A value potentially has different names in different contexts and at different times. In the example here, the_value is the name, inside the function, for the value that is returned. It gets assigned to month; thus, outside the function, month is a name for that value, and print month prints the value. There is nothing in this code that is analogous to input_name in your code, because part of the suggestion is to get rid of that idea completely. You do not pass a parameter in order to get information back; you get it back via the return value.
11

Other people are saying something like this:

def input(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:

def input(input_name, prompt):
    globals()[input_name] = value

1 Comment

Thanks, Elijaheac. Although it is not what Tom was asking for, your response was very useful for me. Please correct me if I am wrong, but I believe this is a good way to set the value of a module's variable that has the same name as the function argument. I have been doing it by having a different name for the argument and for the module's variable, but will from now on use your suggestion.
1

What you want to do is probably a bad practice. Just return input_name from the input function.

def input(param1,param2):
   return value

value = input(param1,param2, ...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.