1

im attempting to load single digit numbers line by line from file in python this code makes an error saying:

line 27, in <module>
environment.environment1.load_map(environmentVector)
TypeError: load_map() takes 1 positional argument but 2 were given

heres the source code: (main.py)

environmentVector = []
environment.environment1 = environment.environment(160, 100, 32, 32)
environment.environment1.load_map(environmentVector)

environment.py:

    def load_map(environmentVector):
        string = ''
        with open('map.txt', 'r') as f:
            for line in f:
                string = f.readline()
                row = []
                for character in string:
                    if character == '0':
                        pass
                    elif character == '1':
                        environmentVector.append(environment)
6
  • Do you have an example of this map.txt data? Commented Nov 27, 2018 at 5:07
  • 0 0 0 0 0 0 0 0 1 Commented Nov 27, 2018 at 5:08
  • zeroes and ones with 1 space between is whats in the map.txt theres about 10 lines of them Commented Nov 27, 2018 at 5:08
  • What's your desired output? Commented Nov 27, 2018 at 5:10
  • i want to store environment objects in a list (each environment having x,y coordinates) to make a map with boundaries where if the player runs into environment the collision will prevent them from walking through the rectangle Commented Nov 27, 2018 at 5:14

1 Answer 1

1

First things first, if load_map is a method of a class, the first argument it needs to take is self. Either add self, or mark load_map as a static method with @staticmethod See this.

About your load_map function: Instead of iterating through the string, split it. It also makes sense to store a map like this to a 2D list. Try this:

def load_map(self, environmentVector):
    with open('map.txt', 'r') as f:
        for line in f:
            nums = list(map(int, line.split()))
            environmentVector.append(nums)
Sign up to request clarification or add additional context in comments.

2 Comments

it still throws the positional argument error even with your code
Did you read the linked SO question and make any modifications? If so, what modifications did you make?

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.