2

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

Jaimen Perez
  • 55
  • 1
  • 4

2 Answers2

3

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()
Calder White
  • 1,287
  • 9
  • 21
  • 1
    `input` returns a `str` by definition in Python 3, so there is no need to do `str(input(...))`. You are still not returning anything in the recursive call. – tripleee Jul 16 '19 at 04:23
  • I know. I just copied his code vertbatim. Thanks for the note about the recursive call though! Didn't catch that. @tripleee – Calder White Jul 16 '19 at 12:10
  • Thank you for that reply. Now I'm receiving the following error, I suppose because, as tripleee pointed out, the way I have my functions set up, they return "None." ----------------------------------------------------------------- File "C:/Users/Jaimen/.spyder-py3/practice7.py", line 34, in remove_key_character return sentence_capitalized.replace(key_character, "") TypeError: replace() argument 1 must be str, not None – Jaimen Perez Jul 16 '19 at 17:13
  • Further on in the traceback you should see which function called `replace_key_character` with the `key_character` argument set to `None`. – tripleee Jul 16 '19 at 17:49
  • I modified my sample code. You can try it again, but I don't think the original should be broken either (if you are running my copy) – Calder White Jul 16 '19 at 18:00
  • Ok. It seems like something is wrong with my copy of Spyder. Anyway thanks for the replies, I understand the general concept which you outlined in your initial paragraph. – Jaimen Perez Jul 16 '19 at 20:08
0

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Idk if it's acceptable to post links here. I tried replying with code and couldn't get the formatting to work at all. https://i.imgur.com/by1dnDH.png – Jaimen Perez Jul 16 '19 at 17:32
  • Images of code are vehemently hated, for good reasons (see e.g. https://idownvotedbecau.se/imageofcode for a rationale). If you want to link to code, post it on a code hosting site like https://ideone.com/ or https://repl.it/ or at least a text hosting site like https://pastebin.com/ ... And maybe explain why we should click through - are you having a problem still, or are you just looking for a review or further feedback? – tripleee Jul 16 '19 at 17:37
  • Ok thank you for the info. I read the page on comment formatting which suggested indenting text by 4 spaces or using control-K, but neither of those worked for me....could you clarify whether this site supports commenting w/code? Otherwise in the future I can use the code hosting sites you mentioned. And to explain, I modified the applicable sections of code as per your feedback (writing a while loop instead of having the function call itself) so I was just wondering if I was on the right track but (that section of) the code seems to work so I guess I don't need further validation.. – Jaimen Perez Jul 16 '19 at 20:00
  • Indeed, there is no support for code formatting in comments. – tripleee Jul 17 '19 at 04:08