0

I am trying to use this function to get user input in creating a dict object that I can use to generate a .json file and the function works right up until you type done it does realize that you typed done and it does display the variable you are creating as its being added on to but when you try to return it it always outputs 'None'. I am using the latest version of python. Me and my teacher have no idea why it doesn't work.

def menu_asset_create_lang(lang):

    # The functions "header, space, and br"
    # are all just for making the text look nice in
    # the command line all they do is print stuff for
    # formatting and clear just does os.system('cls')


    clear()
    header()

    print('Lang Creation: ')
    space()
    print(lang)
    br()
    print('< Done')
    inp1 = labeled_input('Input Lang Code')

    if(inp1 == 'done') or (inp1 == '<'):
        return lang

    inp2 = labeled_input('Input Translation')

    lang.append(inp1 + '\":\"' + inp2)


    menu_asset_create_lang(lang)

# instantiate the variable
lang = []

# feed it into the function and make it
# spit out the generated variable in console
print(menu_asset_create_lang(lang))

also I have tried doing this:

lang = []
langout = menu_asset_create_lang(lang)
print(langout)

Also please understand that I am pretty new to python.

  • Your function has no `return` statement, so it implicitly returns `None` by default – Cory Kramer Oct 02 '19 at 15:25
  • Your recursive case is missing the return statement. Try changing the last line in the function to `return menu_asset_create_lang(lang)` – rdas Oct 02 '19 at 15:25

2 Answers2

0

The recursive call menu_asset_create_lang(lang) is just hanging there: after Python executes it, and after this call returns anything, the function body ends, so that None will be returned implicitly.

Currently, your code is the same as:

def menu_asset_create_lang(lang):
    ...
    menu_asset_create_lang(lang)
    return None

Can you see what you should return instead of None?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • The function prints the variable each time it repeats so you can actively see it change as you add on to it and it displays the correct information the only problem is getting that variable out of this function. – Gannon Burks Oct 02 '19 at 15:54
0
def menu_asset_create_lang(lang):

    # The functions "header, space, and br"
    # are all just for making the text look nice in
    # the command line all they do is print stuff for
    # formatting and clear just does os.system('cls')


    clear()
    header()

    print('Lang Creation: ')
    space()
    print(lang)
    br()
    print('< Done')
    inp1 = labeled_input('Input Lang Code')

    if(inp1 == 'done') or (inp1 == '<'):
        return lang

    inp2 = labeled_input('Input Translation')

    lang.append(inp1 + '\":\"' + inp2)


    return menu_asset_create_lang(lang)

I am hoping you have a recursive return Python returns None by default

Ishan Joshi
  • 487
  • 3
  • 7