Python Forum

Full Version: main function scope issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I created a Python function that take an argument, full name, gets full name's initials and prints them out capitalized but the problem is with my main function I believe at the end of the code in terms of being a global scope vs local scope... I could be wrong though. Any help is appreciated

imports get_initials

""" Given a person's name, returns the person's initials (uppercase) """
    # TODO your code here
 def get_initials(fullname):
     fullname.split()
     name_list = xs.split()
     print(name_list)
     
     get_initials = input("What is your full name?")
     initials = ""
     def main():
         
         for name in name_list:  # go through each name
             initials += name[0].upper()  # append the initial
             return intials 
             
if__name__ == '__main__':
print: initials
main()
[error]invalid syntax (initials.py, line 16)
1. Indentation is messed up
2. A script doesn't import itself
3. you haven't defined name_list
4. Misspelled initials on return line
5. a bunch more

""" Given a person's name, returns the person's initials (uppercase) """
# TODO your code here


def get_initials(fullname):
    name_list = fullname.split()
    print(name_list)

    initials = ""

    # the code below was moved here. if in main, name_list would have been out of scope.
    for name in name_list:  # go through each name
        initials += name[0].upper()  # append the initial
    return initials


def main():
    return get_initials(input("What is your full name?"))

if __name__ == '__main__':
    print(main())