Hello.

Here I go again.

I tried to create a computer enemy for my connect 4 game:

def turn_comp():
        import random
        print 'Computer ist am Zug!'
        playchip = random.randint(0, columns)
        board[0][playchip] = 'O'
        print_board()

Even though I limited the range of randint with 0 and columns (in a 4x4 board columns = 4) the computer chooses a number out of range.

Why? :P

//Edit: If I limit the range to (0, columns - 1) it seems to work perfectly

//Edit2: Is it because the list index starts with 0 (and not with 1)? I guess this is it...

Thanks in advance.

//Edit: If I limit the range to (0, columns - 1) it seems to work perfectly

//Edit2: Is it because the list index starts with 0 (and not with 1)? I guess this is it...

>>> import random
>>> help(random.randint)
Help on method randint in module random:

randint(self, a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.