how to access a variable that's inside a function

Question:

I’m trying to do 4 steps where I ask the user for a “key character” and a string, then capitalize the string, then remove all instances of the key character from the string. Each of these steps is supposed to be its own function. However, steps 3 and 4 rely on accessing variables located in the functions from steps 1 and 2.

I’ve read a few threads on this such as the following…
How to access the variables declared inside functions in python
…which suggests you must “return” the variables after defining them, but I’ve done that (I think) and it hasn’t changed anything.

def main():
    get_key_character()
    get_string()
    sentence_capitalizer()
    remove_key_character()

def get_key_character():
    key_character=str(input("Please enter a SINGLE character to act as key? "))
    if len(key_character)!=1:
        get_key_character()
    else:
        return key_character

def get_string():
    phrase_input=str(input("Please enter a phrase or sentence >=4 and <=500 characters: "))
    if len(phrase_input) <4 or len(phrase_input)>500:
        get_string()
    else:
        return phrase_input

def sentence_capitalizer():
    import re
    sentence_capitalized=(re.sub(r"(^|?|.|!)s*(w)", lambda q: q[0].upper(), phrase_input))
    return sentence_capitalized 

def remove_key_character():
    sentence_capitalized.replace(key_character, "")


main()

error: undefined name phrase_input in def sentence_capitalizer and undefined name key_character in def remove_key_character

Asked By: Jaimen Perez

||

Answers:

What you are missing is that the returned value must be stored in another variable.
For example,

def test():
    x = 1
    return x

def main():
    z = test()
    print(z)

main()

This program will output:

1

You must pass the output of your functions as parameters to the subsequent calls of different functions. I have reworked your code to work like this.

def main():
    key_character = get_key_character()
    phrase_input = get_string()
    sentence_capitalized = sentence_capitalizer(phrase_input)
    removed = remove_key_character(key_character, sentence_capitalized)
    print(removed)

def get_key_character():
    key_character=""
    while len(key_character) < 1:
        key_character=str(input("Please enter a SINGLE character to act as key? "))

    return key_character

def get_string():
    phrase_input=str(input("Please enter a phrase or sentence >=4 and <=500 characters: "))
    if len(phrase_input) <4 or len(phrase_input)>500:
        get_string()
    else:
        return phrase_input

def sentence_capitalizer(phrase_input):
    import re
    sentence_capitalized=(re.sub(r"(^|?|.|!)s*(w)", lambda q: q[0].upper(), phrase_input))
    return sentence_capitalized 

def remove_key_character(key_character, sentence_capitalized):
    return sentence_capitalized.replace(key_character, "")


main()
Answered By: Calder White

if len(key_character)!=1:
get_key_character()

Your function does not return anything (so implicitly returns None) in this case. You want to return get_key_character() (or perhaps more usefully refactor to avoid recursion, i.e. add a while loop and exit it when you receive a valid character, rather than have the function call itself until it receives a valid character).

More generally, you should avoid global variables. Every time a function returns something useful, the calling code should assign that value to a local variable. If you need to pass that value to another function, make that function accept a parameter into another local variable of its own.

Answered By: tripleee
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.