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