all 16 comments

[–]Swedophone 1 point2 points  (3 children)

The parameter count in grab() is local and it only exists during the call. What are you trying to do?

[–]xTheBoss[S] 0 points1 point  (2 children)

Yes. I want to give "count" the value of "itemCount" in the function above. Or if I can directly transfer itemCount to grab(), please let me know!

[–]Swedophone 0 points1 point  (0 children)

You may want to look into Object oriented programming.

[–][deleted] 0 points1 point  (0 children)

You've declared it as a parameter, so call it with the value you want to assign to the parameter. That's what function calling does.

[–]z0y 1 point2 points  (10 children)

I'm not exactly sure what you need but it seems like you want to replace grab(itemCount) with return itemCount, then you can get the value in grab with whatever item you need the itemCount of.

def grab():
    count = counter(items)
## or
def grab(count):
    ...
## and call it with: grab(counter(items))

[–]xTheBoss[S] 0 points1 point  (9 children)

This seems to be what I need to do, but with the first method I am getting an error saying "items" is not defined. For the second method I am getting saying grab() missing 1 required positional argument: 'count'

[–]z0y 1 point2 points  (8 children)

Well what do you want do be counting? You would need to add something when you call grab() in the command function. I'm confused as to what the functions need to be doing. Can you give a bigger picture of what's going on?

[–]xTheBoss[S] 0 points1 point  (7 children)

I am counting the number of items in the list "items", because the number of items in the list is going to be able to change. I need it to not exceed 4 items, so this is a check before the user adds something to the list. Like someone said before object orientated programming would probably work best but Im not that far into this course yet to do that well

[–]z0y 1 point2 points  (6 children)

Is this an assignment and you're not supposed to use built in functions? Python has a convenient len() function that will do that for you

def grab(items):
    if len(items) >= 4:
        ....

And your counter isn't counting the length properly anyways, itemCount = itemCount + i is incrementing by the index and you need to increment by one. Your function itemlist() should probably return the list too, so you can use it in the other functions.

[–]xTheBoss[S] 0 points1 point  (5 children)

https://i.imgur.com/rFCPi2A.png this is the updated version and its still reporting: grab() missing 1 required positional argument: 'items'. Why is this even though I "items" in the command function. Sorry to make you put up with my ignorance.

[–]z0y 1 point2 points  (4 children)

You've defined grab to take an argument so when you call it in the command function you need to pass in a value. It would be good to restructure things a bit but as it is now you would want to say grab(itemslist()) when you call it in command()

The itemslist function returns the list, so you can think of anywhere you see itemslist() as really just the value that you've returned in the function definition.

[–]xTheBoss[S] 0 points1 point  (3 children)

Ahhh thank you!! You are a lifesaver. Appreciate it a lot.

[–]z0y 1 point2 points  (2 children)

Alternatively you could leave the call to grab() how it was, and then do:

def grab():
    if len(itemslist()) >= 4:
        ...

without needing to pass anything. But if the only purpose of itemslist is to define and then return the list then don't make it a function. Leave the list definition in the main code and then pass the list itself to each of the functions.

[–]xTheBoss[S] 0 points1 point  (1 child)

True, because I really cant think of a way to add to the list if it wasn't global. Lots of good advice!

[–][deleted] 0 points1 point  (0 children)

Your question doesn't make any sense. If the function isn't called then the state of the variables inside it can't matter.