Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

how do I get a complicated python function with nested functions inside it to return a value? [closed]

$
0
0

I am trying to create a combined set of functions (so functions inset in other functions) to create an input field using Python play. I want this input field function to be fully standing on its own. I'm trying to get it to return a value, but due to the nesting of the functions it doesn't want to work.here is an explanation of the code, to make it easier to read:the enveloping function, main_loop, is called below, in the function runner.you first need to click the input field to start typing, after which the standard text is deleted and you have an empty slate to typeafter typing, you click enter once. you then get feedback in the terminal, asking to press enter if you want to finish and press backspace to go back to typing.if you press enter, you can't type anymore and the delete_input_field_variables function is used to remove all variables (this is to create a clean slate to use the overlapping function multiple times). at this point, the code should return the input.

I have left out the function creating the variables because that one works seperately and doesn't really interfere with the rest of the code. if needed, comment if you think it would benefit to have that function in here.

the main problem, I think, is the fact that I can't get the function to return anything, while I try to also not have it return values full-time, thus the if-statements.what keeps happening is that the function just doesn't return a value. I am pretty new to these nesting functions (this is the first time I am attempting them), and it is only my second time with return functions, so I have no clue how or even if what I'm asking is possible. any help is appreciated.

following will be the code.

import playinput_start = 'press to clear field and start typing'async def main_loop(x_coordinate_input_field, y_coordinate_input_field, standard_text_input_field, input_field_height, input_field_width, explanation_text_words):    @play.repeat_forever    async def loop_in_loop():        await input_field_create_and_starting_values(x_coordinate_input_field, y_coordinate_input_field, standard_text_input_field, input_field_height, input_field_width, explanation_text_words)        @input_background.when_clicked        def allow_typing():            global allow_type            if 'allow_type' in globals():                if allow_type == 0:                    allow_type = 1                    if input_field.words == input_start:                        input_field.words = ''                elif allow_type == 1:                    print("allow_type is 1 when clicked")        async def delete_input_field_variables():            global stop_create, input_field, input_background, allow_type, check_input, character_list, overall_allow, explanation_text_screen            del stop_create, input_field, input_background, allow_type, check_input, character_list, overall_allow, explanation_text_screen            if 'allow_type' in globals():                print(f"This is allow_type right after delete: {allow_type}")            await play.timer(seconds=3)        async def check_allow_type():            if 'allow_type' in globals():                print('hallo')                print(f"allow_type is {allow_type}")                await play.timer(seconds=2)        await check_allow_type()        @play.when_any_key_released        async def keypress(key):            global input_field, check_input, allow_type, overall_allow, explanation_text_screen, input, return_value_activation            if 'overall_allow' in globals() and overall_allow == 1:                if key in character_list:                    wait_time = 0.1                    if allow_type == 1:                        if check_input == 0:                            if key != 'backspace' and key != 'space' and key != 'enter':                                input_field.words += key                                allow_type = 2                                await play.timer(seconds=wait_time)                            if key == 'backspace':                                input_word = input_field.words                                input_word = input_word[:-1]                                input_field.words = input_word                                allow_type = 2                                await play.timer(seconds=wait_time)                            if key == 'space':                                input_field.words += ''                                allow_type = 2                                await play.timer(seconds=wait_time)                            if key == 'enter':                                print('Are you sure you want to enter your input?')                                print('Press enter to confirm, press backspace to continue editing your input')                                allow_type = 0                                await play.timer(seconds=wait_time)                                check_input = 1                        elif check_input == 1:                            if key == 'backspace':                                check_input = 0                                allow_type = 2                            elif key == 'enter':                                input = input_field.words                                print(f'This "{input}" is your input')                                input_field.hide()                                input_background.hide()                                explanation_text_screen.hide()                                allow_type = 0                                overall_allow = 0                                return_value_activation = 1                                await delete_input_field_variables()                        await play.timer(seconds=wait_time / 5)                    allow_type = 1    if return_value_activation == 1:        print("returning input value now")        return(input)@play.when_program_startsasync def runner():        print_test1 = await main_loop(0,0,input_start,100,play.screen.width - 200,'this is print_test1')        @play.repeat_forever        async def loop_print_test1():            global return_value_activation            if return_value_activation == 1:                await play.timer(seconds = 1)                print(f"print_test1 is {print_test1}")play.start_program()

Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>