0

I am trying to get col_number and user_input to be recognized outside of the input function.

The only way I seem to be able to use the col_number outside the input function is if I define a global variable inside - which doesn't seem right. I have tried placing the statement above the def get_input but get a 'col_number might be referenced before assignment.

I need to use the user_input as a txt string for a header on a graph but don't understand how to pass it out. The print statement last line in the code gives me an unresolved reference.

Any suggestions please.

 col_number = int
def get_input(prompt):
        #global col_number
        #locals col_number
        global col_number
        while True:
            user_input = input(prompt).lower()
            if user_input in ('apples', 'pears', 'oranges', 'quit'):
    # the user = int(0),int(1), int(2) values just assign a different column numnber
                if user_input == 'apples':
                    col_number = 0
                if user_input == 'pears':
                    col_number = 1
                if user_input == 'oranges':
                    col_number = 2
            return col_number, user_input

print(get_input(prompt='Enter apples, pears, oranges or q to quit'))
print(user_input)
2
  • 1
    col_number = int isn't what you think it is, it looks like you're trying to reassign the type int to col_number. Did you mean col_number = int()? Commented Apr 7, 2020 at 15:18
  • @G.Anderson is right. If you're trying to use a type hint, you want col_number: int Commented Apr 7, 2020 at 15:23

4 Answers 4

4

You are not defining the global variable inside. The global statement is telling the Python interpreter to use the col_number object created in the global scope instead of creating a new local scope object named col_number.

A better way to approach this would be to assign the returned objects from your function instead of pre-defining them:

def get_input(prompt):
   while True:
        user_input = input(prompt).lower()
        if user_input in ('apples', 'pears', 'oranges', 'quit'):
            if user_input == 'apples':
                col_number = 0
            if user_input == 'pears':
                col_number = 1
            if user_input == 'oranges':
                col_number = 2
        # I think this next line should be indented?
        # I left it here as is to reference your code but it'll error out
        # if the input is not valid since col_number will not be created.
        return col_number, user_input

# Assign and make use of your returned objects!
my_col, my_input = get_input('Enter apples, pears, oranges or q to quit')
print(my_col)
print(my_input)

It's best to avoid referencing global variables within functions as it can get messy the more complex your code becomes.

Sign up to request clarification or add additional context in comments.

Comments

1

You never declare user_input as a global variable. You could fix this problem by adding user_input="" before the function and adding global user_input after the other global statement, but this is an unpythonic way to return values from a function. Instead, you should assign the result of the function to variables, for example: col_number, user_input = get_input(prompt='Enter apples, pears, oranges or q to quit'). By doing this, you can access these values and also remove col_number = int and global col_number as they are no longer needed.

Comments

1

To get user_input out of get_input you have a couple options:

Declare a variable from the function:

col_num, usr_inpt = get_input(prompt='Enter apples, pears, oranges or q to quit')
print(usr_inpt)

Make user_input a global variable.

col_number = 0
user_input = None
def get_input(prompt):
    global col_number
    global user_input
    while True:
        user_input = input(prompt).lower()
        if user_input in ('apples', 'pears', 'oranges', 'quit'):
            # the user = int(0),int(1), int(2) values just assign a different column numnber
            if user_input == 'apples':
                col_number = 0
            if user_input == 'pears':
                col_number = 1
            if user_input == 'oranges':
                col_number = 2
        return col_number, user_input

print(get_input(prompt='Enter apples, pears, oranges or q to quit'))
print(user_input)

But, as @r.ook pointed out, it's usually a bad idea to use globals.

Comments

0

Thanks for that - ive been looking at that all day1 for that its working now I am trying to include the 'break' line to quit out for the final part. It works but crashes out instead. I am missing something else.

def get_input(prompt):

    while True:
        user_input = input(prompt).lower()
        if user_input in ('apples', 'pears', 'oranges', 'quit'):
            # the user = int(0),int(1), int(2) values just assign a different column numnber
            if user_input == 'apples':
                col_number = 0
            if user_input == 'pears':
                col_number = 1
            if user_input == 'oranges':
                col_number = 2
            if user_input == 'quit':
                break
            return col_number, user_input


my_col, my_input = get_input(prompt='Enter apples, pears, oranges or q to quit')
print(my_input)

1 Comment

Instead of break, use col_number = None or something, so that your return statement won't error out. Otherwise it'll break the loop and return nothing for your function, making your object assignment for the returned objects giving unpacking error.

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.