Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
main function scope issues
#1
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)
Reply
#2
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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,524 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 795 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,739 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Function global not readable by 'main' fmr300 1 1,296 Jan-16-2022, 01:18 AM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,506 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,782 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Variable scope - "global x" didn't work... ptrivino 5 2,979 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,775 Dec-14-2020, 11:21 PM
Last Post: muzikman
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 5,865 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  how to run another function from main file Mekala 3 2,476 Aug-09-2020, 04:41 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020