0

I want to pass an empty array from main function to a user defined function and inside that function the inputs will be taken into the array from the user and the array will return to the main function when called. The inputs will be in string.

I am new to Python. Can someone please show me how to write this code in Python?

martineau
  • 119,623
  • 25
  • 170
  • 301
TechGirl
  • 9
  • 3

1 Answers1

0

This is a bit of a hard question to answer without an example, but I will try my best. So let's say you want to call a function create_list and have it make your list. You could do it a few ways. This first example is how you could create the list inside of the create_list.

def create_list(arg1, arg2, arg3):
    result = []
    result.append(arg1)
    result.append(arg2)
    result.append(arg3)
    # Or whatever the code is to generate the list
    return result

my_list = create_list("Hello", "world", "!")

print(my_list)

Alternatively, you could create a list in your main function and have create_list modify it.

def create_list(list_arg, arg1, arg2, arg3):
    list_arg.append(arg1)
    list_arg.append(arg2)
    list_arg.append(arg3)
    # Or whatever the code is to generate the list

my_list = []

create_list(my_list, "Hello", "world", "!")

print(my_list)

There are many other ways to do this depending on what you are looking for exactly but those are the most basic and will work in a majority of cases.

Bryce
  • 121
  • 3
  • Could you please correct the line(s) that is wrong in this following code: https://pastebin.pl/view/9023a6f9 – TechGirl Nov 11 '20 at 08:43
  • That link does not work for me for some reason. Either way, Stack Overflow is generally not for asking someone to fix code, but to ask questions so that you can learn how to fix it yourself. – Bryce Nov 11 '20 at 20:57