Python Forum

Full Version: Simple Function Problem, please help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
def columncheck(c):
    x=5
    pos=False
    while pos != True:
        if dboard[x][(4*c)-1]== " ":
            pos=True
        elif dboard[x][(4*c)-1] != " ":
            x-=1
    return x
When this function returns x, x is always 5.

Why?

It has been annoying me for days.
Helloo?
Hi! Big Grin
how are you calling it, what's the value of c?
def columncheck(c):
   x=5
   pos=False
   while not pos:
       if dboard[x][(4*c)-1]== " ":
           pos=True
       else:
           x-=1
   return x
My brain is fried... I tweaked the function to make it a little clearer, but if there's an obvious reason for it not working, I don't see it.  What's dboard?  How is it defined/iniitialized and set?
c is an integer between 1 and 7, dboard is a 2d array, consisting of variables which contain strings.

The function is called this way:

column=int(input("Choose a column: "))
columncheck(column)
So, i create several strings in variables and print out portions of your code like (excuse the global):
str1 = 'hello Mabel'
str2 = 'Hi ya Fred'
str3 = 'I am a string'
str4 = 'so am i'
str5 = 'how about some food'
str6 = 'Not right now'
str7 = 'yubba dubba do'

c = 5
dboard = [str1, str2, str3, str4, str5, str6, str7]

def columncheck(c):
   global dboard
   x = 5
   pos = False
   while pos != True:
       print('x: {}'.format(x))
       y = dboard[x]
       x1 = 4 * c - 1
       print(f'y: {y}')
       print(f'x1: {x1}')
       print(f'y[x1]: {y[x1]}')
       if dboard[x][(4*c)-1] == " ":
           pos = True
       elif dboard[x][(4*c)-1] != " ":
           x -= 1
       print('x: {}'.format(x))
   print('x: {}'.format(x))
   return x

column = int(input('Choose  a column: '))
columncheck(column)
and get some results:
Output:
Choose  a column: 2 Traceback (most recent call last):  File "M:/python/forum/Old/junk5.py", line 32, in <module>    columncheck(column)  File "M:/python/forum/Old/junk5.py", line 22, in columncheck    print(f'y[x1]: {y[x1]}') IndexError: string index out of range x: 5 y: Not right now x1: 7 y[x1]: h x: 4 x: 4 y: how about some food x1: 7 y[x1]: u x: 3 x: 3 y: so am i x1: 7
and then the crash (traceback occurs after x1: 7)
row6="|   |   |   |   |   |   |   |"
row5="|   |   |   |   |   |   |   |"
row4="|   |   |   |   |   |   |   |"
row3="|   |   |   |   |   |   |   |"
row2="|   |   |   |   |   |   |   |"
row1="|   |   |   |   |   |   |   |"
dboard=[row6,row5,row4,row3,row2,row1]
l="-----------------------------"


def drawboard():
    print("| 1 | 2 | 3 | 4 | 5 | 6 | 7 |")
    print(l)
    for x in dboard:
        print(x)
        print(l)

def columncheck(c):
    x=5
    pos=False
    while pos != True:
        if dboard[x][(4*c)-1]== " ":
            pos=True
        elif dboard[x][(4*c)-1] != " ":
            x-=1
    return x
That is my part of my code. It will be a connect 4 game, the function should check if the most bottom row full. Hopefully that adds some context. The problem is that no matter what, the function columncheck©, will ALWAYS return five, whether the position is taken or not.
Please help! Wall Wall Cry Cry
From post 4:
how are you calling it, what's the value of c?
Pages: 1 2