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)
8
  • 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. Commented Oct 21, 2019 at 18:17
  • 1
    Please fix the indentation. - Formatting help ... more Formatting ... Formatting sandbox Commented Oct 21, 2019 at 18:18
  • The list open_numbers[] Commented Oct 21, 2019 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 Commented Oct 21, 2019 at 18:19
  • 1
    I understand what you're asking, but in the future you really need to make a minimal reproducible example including how you're calling myfunction with open_numbers, and the full error message with traceback. See How to Ask for more tips. Commented Oct 21, 2019 at 18:25

3 Answers 3

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]
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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()

1 Comment

Using a mutable default argument is probably a bad idea. For example try running for _ in range(2): print(myfunction())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.