0

I have small a python script looking something like this:

def number1():
    x = 1
    open_numbers = []
    open_numbers.append(x)
    return open_numbers

def myfunction(open_numbers):
    y = 1
    open_numbers.append(y)

I would like to call the myfunction in the the end of the script. Using

myfunction()

But it keeps telling me missing 1 required positional argument: 'open_numbers'

Tried passing the argument and got name 'open_numbers' is not defined

I plan to add more functions later and run them the same way

function(arg)
function2(arg)
function3(arg)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    What data are you trying to give to `myfunction`? `open_numbers` only exists inside of `number1`. You need to use the return from that function if you want to use it. – Carcigenicate Oct 21 '19 at 18:17
  • 1
    Please fix the indentation. - [Formatting help](https://stackoverflow.com/help/formatting) ... [more Formatting](https://stackoverflow.com/editing-help) ... [Formatting sandbox](https://meta.stackexchange.com/questions/3122/formatting-sandbox) – wwii Oct 21 '19 at 18:18
  • The list open_numbers[] – Viktor Berg Oct 21 '19 at 18:19
  • 1
    if you call the function `myfunction()` without any arguments, you'll get the first error: `1 required positional argument: 'open_numbers'`. If you call it with the argument `open_numbers`, you need to define `open_numbers` out of the scope of the function or define it as a new local variable inside the function – Pranjal Aswani Oct 21 '19 at 18:19
  • Possible duplicate of [Python 3 Exception: TypeError: function missing 1 required positional argument: 'words'](https://stackoverflow.com/questions/35968844/python-3-exception-typeerror-function-missing-1-required-positional-argument) – wwii Oct 21 '19 at 18:20
  • 1
    I understand what you're asking, but in the future you really need to make a [mre] including how you're calling `myfunction` with `open_numbers`, and the full error message with traceback. See [ask] for more tips. – wjandrea Oct 21 '19 at 18:25
  • I fixed the indenting cause it seems like your source code is fine, you just made a typo while writing the question. Otherwise you would have gotten an `IndentationError` – wjandrea Oct 21 '19 at 18:28
  • You might want to read about [shadowing](https://stackoverflow.com/q/20125172/4518341) and [scoping](https://stackoverflow.com/q/291978/4518341) – wjandrea Oct 21 '19 at 18:33

3 Answers3

1

Solution

First of all, your code was not properly indented. I have corrected that.
The function myfunction takes in a list (open_numbers) as input and should return it as well.

I have passed in the output of number1() as the input to myfunction(). This should create a list: [1, 1]. And that's what it did.

def number1():
    x = 1
    open_numbers = []
    open_numbers.append(x)
    return open_numbers


def myfunction(open_numbers):
    y = 1
    open_numbers.append(y)
    return open_numbers

myfunction(number1())

Output:

[1, 1]
CypherX
  • 7,019
  • 3
  • 25
  • 37
1

you need to pass in an object to your function. you can call your function with an empty list if you want:

a = []
myfunction(a)
0

You might want to define default parameter, and return the updated value

def myfunction(open_numbers = []):
    y = 1
    open_numbers.append(y)
    return open_numbers

Then you can call it with passing parameter myfunction([1]) or without myfunction()

Jacek Rojek
  • 1,082
  • 8
  • 16
  • [Using a mutable default argument is probably a bad idea](https://docs.quantifiedcode.com/python-anti-patterns/correctness/mutable_default_value_as_argument.html). For example try running `for _ in range(2): print(myfunction())` – wjandrea Oct 21 '19 at 18:37