Python Forum
Simple Function Problem, please help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Function Problem, please help
#1
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.
Reply
#2
Helloo?
Reply
#3
Hi! Big Grin
Reply
#4
how are you calling it, what's the value of c?
Reply
#5
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?
Reply
#6
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)
Reply
#7
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)
Reply
#8
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.
Reply
#9
Please help! Wall Wall Cry Cry
Reply
#10
From post 4:
how are you calling it, what's the value of c?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 732 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,247 Jun-14-2022, 08:35 PM
Last Post: thesquid
Big Grin question about simple algorithm to my problem jamie_01 1 1,696 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  simple login problem MMOToaster 2 2,414 Feb-25-2020, 09:28 AM
Last Post: MMOToaster
  got SyntaxError while building simple function zarize 2 2,137 Feb-14-2020, 10:51 AM
Last Post: zarize
  Problem with simple 3D Vektor calculation Pythocodras 0 1,716 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Simple statistics with range function Pythonlearner2019 2 2,131 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Simple problem. looking for an efficient way silverchicken24 3 2,345 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Cannot get simple i/o to function. jerryi 10 6,732 Jul-27-2019, 06:22 PM
Last Post: jerryi
  simple string & input problem kungshamji 5 3,682 Jun-23-2019, 03:54 PM
Last Post: kungshamji

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020