0

I'm trying to append inputs to a list in another python file. I think I have the order correct but the error message I get is AttributeError: module 'inventory' has no attribute 'pick' when I try it out.

main.py:

import inventory

choice = input("--> ")

if "inv pick" in choice:
    inventory.pick()

inventory.py:

import main

backpack = []

def pick():
    """
    Function for picking up things
    """
    backpack.append(main.choice)
    print(backpack)

If I make write the string "inv pick flower" end hit enter I get the error message instead of the printed content of the list 'Backpack'. Maybe I should use .extend instead of .append but neither of it works right now. Any pointers perhaps?

Regards

6
  • 1
    I think your problem is described at stackoverflow.com/questions/744373/… Commented Oct 8, 2018 at 22:33
  • @MichaelButscher So they can't import each other? Then I have to use a third .py-file to somehow store and process? Commented Oct 8, 2018 at 22:40
  • 1
    Read here there's a fix. Commented Oct 8, 2018 at 22:40
  • @IrfanuddinShafi It worked insofar as there are no error messages but nothing gets printed though... Commented Oct 8, 2018 at 22:49
  • Why don't you use the choice as a parameter for pick()? As it is, it's bad design. Commented Oct 8, 2018 at 22:49

1 Answer 1

2

The following is a much better way to implement what you're trying to achieve without any problematic circular imports.

main.py:

import inventory

choice = input("--> ")


inventory.pick(choice)

inventory.py:

backpack = []

def pick(choice):

    backpack.append(choice)
    print(backpack)
Sign up to request clarification or add additional context in comments.

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.