Hướng dẫn how to append to a list in another file python - cách nối vào một danh sách trong một tệp python khác

Tôi đang cố gắng nối các đầu vào vào một danh sách trong một tệp Python khác. Tôi nghĩ rằng tôi có đúng thứ tự nhưng thông báo lỗi tôi nhận được là AttributeError: module 'inventory' has no attribute 'pick' khi tôi dùng thử.

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)

Nếu tôi viết chuỗi "Inv Pick Flower", tôi sẽ nhận được thông báo lỗi thay vì nội dung được in của danh sách 'ba lô'. Có lẽ tôi nên sử dụng .extend thay vì .Append nhưng cả hai đều không hoạt động ngay bây giờ. Bất kỳ con trỏ có lẽ?

Trân trọng

Hỏi ngày 8 tháng 10 năm 2018 lúc 22:28Oct 8, 2018 at 22:28

Hướng dẫn how to append to a list in another file python - cách nối vào một danh sách trong một tệp python khác

6

Sau đây là một cách tốt hơn nhiều để thực hiện những gì bạn đang cố gắng đạt được mà không có bất kỳ nhập khẩu thông tư có vấn đề nào.

main.py:

import inventory

choice = input("--> ")


inventory.pick(choice)

Inventory.py:

backpack = []

def pick(choice):

    backpack.append(choice)
    print(backpack)

Đã trả lời ngày 8 tháng 10 năm 2018 lúc 23:10Oct 8, 2018 at 23:10

Đầu ra: & nbsp; & nbsp;

Xem thảo luận

Cải thiện bài viết

  • Lưu bài viết
  • Đọc
  • Đầu ra: & nbsp; & nbsp;

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết
    Example 
     

    Input :
    file1.txt
    file2.txt
    
    Output :
    Content of first file (before appending) - geeksfor
    Content of second file (before appending) - geeks
    Content of first file (after appending) - geeksforgeeks
    Content of second file (after appending) - geeks

    Đọc 
     

    1. Bàn luận
    2. Có hai tên tệp được nhập từ người dùng, nhiệm vụ là nối nội dung của tệp thứ hai vào nội dung của tệp đầu tiên.Example & nbsp; & nbsp;
    3. Thuật toán: & nbsp; & nbsp;
    4. Nhập tên của các tập tin.
    5. Mở cả hai tệp trong chế độ chỉ đọc bằng hàm Open ().
    6. In nội dung của các tệp trước khi thêm vào hàm đọc ().
    7. Đóng cả hai tệp bằng hàm đóng ().
    8. Mở tệp thứ nhất ở chế độ phụ lục và tệp thứ hai ở chế độ đọc.
    9. Đóng cả hai tập tin.

    Giả sử các tệp văn bản File1.txt và File2.txt chứa dữ liệu sau.File1.TXT & nbsp; & nbsp;
    file1.txt 
     

    Hướng dẫn how to append to a list in another file python - cách nối vào một danh sách trong một tệp python khác

    file2.txt   
     

    Hướng dẫn how to append to a list in another file python - cách nối vào một danh sách trong một tệp python khác

    python3

    firstfile = input(____1010

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    2= input(
    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    6
    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    1

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    8=
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    0
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    1
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    222

    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    4=
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    0
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    7
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    222

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    0(
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    2
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    3

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    0(
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    6
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    7

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    8

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    9

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    8=
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    0
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    1
    Input :
    file1.txt
    file2.txt
    
    Output :
    Content of first file (before appending) - geeksfor
    Content of second file (before appending) - geeks
    Content of first file (after appending) - geeksforgeeks
    Content of second file (after appending) - geeks
    44

    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    4=
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    0
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    7
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    222

    AttributeError: module 'inventory' has no attribute 'pick'2

    AttributeError: module 'inventory' has no attribute 'pick'3AttributeError: module 'inventory' has no attribute 'pick'4

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    1

    AttributeError: module 'inventory' has no attribute 'pick'6AttributeError: module 'inventory' has no attribute 'pick'4

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    1

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    0(firstfile 1
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    3

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    0(firstfile 5
    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    7

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    8

    backpack = []
    
    def pick(choice):
    
        backpack.append(choice)
        print(backpack)
    
    9

    import main
    
    backpack = []
    
    def pick():
        """
        Function for picking up things
        """
        backpack.append(main.choice)
        print(backpack)
    
    8=
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    0
    import inventory
    
    choice = input("--> ")
    
    
    inventory.pick(choice)
    
    1
    Input :
    file1.txt
    file2.txt
    
    Output :
    Content of first file (before appending) - geeksfor
    Content of second file (before appending) - geeks
    Content of first file (after appending) - geeksforgeeks
    Content of second file (after appending) - geeks
    44
     
     

    Hướng dẫn how to append to a list in another file python - cách nối vào một danh sách trong một tệp python khác