1

This is a very basic question, but I can't seem to extend an array with a new item that is added within a function. I haven't gotten push to work either, so I think my array is just not being addressed by the other functions.

The array is declared as a global because otherwise function(s) do not appear to be able to see it. There is probably a much better way, but this is working in a few ways.

this is one set of things I've tried:

        # addTo.insert(0, theItem)
        # weeklytasks.append(theItem)
        # print(addTo) 
        # def askForItem(theItem):
        #     addTo.push(theItem)
        #     print(addTo)
        #

but no luck.

 def initializer():
            print("hello!")
            print("do you want to view the current list, or add a new item?")

setting up some arrays

        global weeklyTasks = ['no items'];
        global monthlyTasks = ['no items']
        global quarterlyTasks = ['no items'];

trying to extend

        quarterlyTasks.extend('the item')
        print(quarterlyTasks);

Neither of these work

        #monthlyTasks.push(0,"task1")
        #monthlyTasks.append("item")

set a var for user's input

        global theItem
        addTo = ""
        theItem = ""


        import string
        print('addTo in globals, ' 'addTo' in globals()) 

takes an item and checks its timeframe

        def evaluateInput(timeframe):

            global addTo;
            global theItem;
            if timeframe == "Weekly":
                addTo = "weeklyTasks";
                printDestination();
                weeklyTasks.extend(theItem); # why does [].extend not work here?

            if timeframe == "Monthly":
                 addTo = "monthlyTasks"
                 monthlyTasks.insert(0,theItem)
                 printDestination()
            if timeframe == "Quarterly":
                 addTo = "quarterlyTasks"
                 quarterlyTasks.insert(0,theItem)
                 printDestination()

follow up with a request for the timeframe

      def getTimeframe(text): 
        if "add" in text: 
            timeframe = input("Where does this entry go? \n Weekly, Monthly or Quarterly?: ")
            evaluateInput(timeframe) # sends the timeframe to evaluateInput

            # next, ask for the item
            theItem = input('what is the item?')

print a confirmation of what is being added where

        def printDestination():
            print("The item (" + theItem+") will be added to " + addTo)
            # prints where something is going
            print("the "+addTo+" list now contains:") # good, this worked
            print(weeklyTasks) #good, this worked (but the method to push doesn't work)
3
  • Are you defining your globals within the initializer function? It's a bit hard to tell. If so, remember you need to call the initializer function before the globals will be available. (I would recommend fixing the indentation in your examples - it's a bit hard to tell what code is in what block.) Commented Sep 3, 2016 at 5:13
  • 1
    What do you mean they "don't work?" How don't they work Commented Sep 3, 2016 at 5:25
  • Python doesn't use semi colons to end a line Commented Sep 3, 2016 at 9:25

2 Answers 2

3

A working example of all mentioned functions. global is not required, because the tasklist is not re-assigned in the function, only its methods are called.

tasklist = ['one']

def testfunc():
    # no global required
    tasklist.append('two')
    tasklist.extend(['three', 'four'])
    tasklist.insert(0, 'zero')

testfunc()
print(tasklist)
# ['zero', 'one', 'two', 'three', 'four']

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's the working solution in python 3.2 to add items to list in function:

weeklyTasks = ['No work Sunday']
def AddWeeklyTask():
    weeklyTasks.extend(['Work On'])
    weeklyTasks.append('Do not work on weekends')
    weeklyTasks.insert(2,'Friday')

print(weeklyTasks)
AddWeeklyTask()
print(weeklyTasks)

Output:

['No work Sunday']
['No work Sunday', 'Work On', 'Friday', 'Do not work on weekends']

There is no need to declare list as global

Comments

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.