0

I'm terribly sorry I have to ask such a ridiculously simple question, but I've been looking for the answer for awhile now, I feel like an idiot, but I cannot seem to get this to work.

I simply want to create an array with 10 variables, each defined by the user, and then have the option to add them all together. I can't even get the first parts correctly though. Here is my code so far:

def makeArray():
    a = [0,1,2,3,4,5,6,7,8,9]
    a[0] = input("Please input 1st number: \n")
    a[1] = input("Please input 2nd number: \n")
    a[2] = input("Please input 3rd number: \n")
    a[3] = input("Please input 4th number: \n")
    a[4] = input("Please input 5th number: \n")
    a[5] = input("Please input 6th number: \n")
    a[6] = input("Please input 7th number: \n")
    a[7] = input("Please input 8th number: \n")
    a[8] = input("Please input 9th number: \n")
    a[9] = input("Please input 10th number: \n")
def main():
    makeArray()
    print(a[2])
main()

Whenever I run it though after entering the 10 values I get "Global name 'a' is not defined." can anyone please help me?

3 Answers 3

2

You need to return the array, as such:

def makeArray():
    a = []
    for x in xrange(10):
        a.append(input("Please input the %d number: " % x))
    return a

def main():
    my_array = makeArray()
    print(my_array[2])
main()
Sign up to request clarification or add additional context in comments.

1 Comment

The OP's original method was not idiomatic and was the long way of doing it, but it was syntactically correct. All that was missing before was the return statement and the variable assignment in the main function. I just wanted to point this out so that the OP knows exactly what they did wrong.
1

This is a smaller version of your code with the same error:

def makeList():
    a=[0,1,2]

def main():
    makeList()
    print a[1]

main() 

You can fix it like this:

def makeList():
    a=[0,1,2]
    return a

def main():
    a=makeList()
    print a[1]

main() 

BTW -- 'arrays' are called 'lists' in Python, unless you are specifically using the array module or numpy to create arrays...

1 Comment

This helps a lot, thank you Edit: How do I make each value based off input though?
1

You have 2 a's. One who's scope is in makeArray and the other that is in main.

in main:

a = list ()
makeArray (a)

Should get things working nicely. (Take out the initialization in makeArray.) Note that all of the elements of the array in your program will be strings.

2 Comments

I'm sorry, I must be misunderstanding.. I replaced the code in main with yours and it still doesn't work "makeArray() takes no arguments (1 given)" and how can I make the array values integers?
Ah, yes. Of course you need to add a as a paremeter in makeArray (a).

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.