Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation checking
#11
It's the exact same problem you had with create_board, just with is_valid_location. You are not assigning the return value when you call the function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
So what code would I need to add to my newest code, I've already put return invalid and the rest are global variables (I think)?

p.s. thanks for all this help
Reply
#13
Avoid global variables. Use parameters, return values, and assignments.

The problem is exactly the same, so the solution is exactly the same. Put in a little effort here and try to come up with the code yourself.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
Why doesn't returning the parameters fix my issue?

def is_valid_location(column, column_choice, invalid):
    if (column-1) < column_choice < (column+1):
        NO_of_spaces_left = NO_of_spaces_left - 1 
        invalid = False
    else:
        invalid = True
    return column
    return column_choice
    return invalid
Reply
#15
Please can someone help me out with my code I'm stuck with the validating part of my code which checks if the column selected is within the chosen size of my 2D array created on board_create()

My code keeps me stuck in a loop that just sends me back player 1 enter when I enter the correct input doesn't move onto player 2, can someone please fix this for me, thank you

turn = 0
invalid = True
game_over = False

def board_create(column, row):
    board = [[' '] * column for row in range(row)]
    return board

def is_valid_location(column, column_choice, invalid, NO_of_spaces_left):
    if (column-1) < column_choice < (column+1):
        NO_of_spaces_left = NO_of_spaces_left - 1 
        invalid = False
        return column
        return column_choice
        return NO_of_spaces_left
        return invalid
    else:
        invalid = True
        return column
        return column_choice
        return NO_of_spaces_left
        return invalid

row = int(input("How many rows: "))
column = int(input("How many columns: "))
NO_of_spaces_left = (row*column)
board = board_create(column, row)

while not game_over or NO_of_spaces_left != 0 :
    if turn == 0:
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
            column = int(column)
            is_valid_location(column, column_choice, invalid, NO_of_spaces_left)
    else:
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 2 make your selection (1-"+column+"):"))
            column = int(column)
            is_valid_location(column, column_choice, invalid, NO_of_spaces_left)
    turn = turn + 1
    turn = turn % 2
Reply
#16
(Aug-15-2019, 04:37 PM)Help_me_Please Wrote: while invalid == True:
You're stuck in a loop, because you're looping until it's valid, but you never change the invalid variable.


(Aug-15-2019, 04:37 PM)Help_me_Please Wrote:
def is_valid_location(column, column_choice, invalid, NO_of_spaces_left):
    if (column-1) < column_choice < (column+1):
        NO_of_spaces_left = NO_of_spaces_left - 1 
        invalid = False
        return column
        return column_choice
        return NO_of_spaces_left
        return invalid
    else:
        invalid = True
        return column
        return column_choice
        return NO_of_spaces_left
        return invalid
This function perhaps is tricking you into thinking you're changing the invalid variable, but you're not. You're creating local variables that happen to have the same name as the global variable, the values of the two different variables are unrelated to each other. Which is one of the reasons you should avoid global variables, as they can easily make things like this hard to debug (you never change variables that you think are changed often).

So let's rewrite that function to look like this:
def is_valid_location(column, column_choice, invalid, NO_of_spaces_left):
    if (column-1) < column_choice < (column+1):
        return False
    return True
And then use it like so:
invalid = True
while invalid == True:
    column = str(column)
    column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
    column = int(column)
    invalid = is_valid_location(column, column_choice, invalid, NO_of_spaces_left)
Reply
#17
lines 8 and 9 are never executed (once the function return on line 7). Anyway, why you return line column and column_choice?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#18
You can only have one return statement. After any return statement, the execution of the function stops. To return multiple values use tuple syntax: return value1, value2, value3.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#19
I have emended the code as appropriate but I'm still stuck in the loop:
turn = 0
game_over = False

def board_create(column, row):
    board = [[' '] * column for row in range(row)]
    return board

def is_valid_location(column_choice, invalid):
    if (column-1) < column_choice < (column+1):
        return False, column_choice, invalid
    else:
        return True, column_choice, invalid
    
row = int(input("How many rows: "))
column = int(input("How many columns: "))
board = board_create(column, row)

while not game_over:
    if turn == 0:
        invalid = True
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 1 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, invalid)
    else:
        while invalid == True:
            column = str(column)
            column_choice = int(input("Player 2 make your selection (1-"+column+"):"))
            column = int(column)
            invalid = is_valid_location(column_choice, invalid)
    turn = turn + 1
    turn = turn % 2
Reply
#20
I'd like to help but I am not familiar with game 4 in row and I have very little understanding what the code should do.

Therefore only nitpicking.

name 'game_over'. Wouldn't it be more logical to have 'game'.
Instead of:

game_over = False
while not game over:
To have:

game = True
while game:
I can't see that game_over is used anywhere except in while loop threfore one can skip it altogether:

while True:
In order to break out from while loop you must set looping condition so that it will stop if criteria is met. There is no change of value game_over and this loop will go on indefinitely.

There are nested while loops and breaking out from inner and outer can be tricky. Recommended practice is to use function instead of nested while-loop.

There are also (unnecessary) conversions and rows.

I would recommend to have a plan along those lines:

# validate user input for creating board
# validate user input of choices and .... /what should happen?
# how to determine when winning state is achieved (is this a game where should be a winner?)
# implement whatever rules the game has
# etc
# output winner

EDIT:

There is nice tutorial on this forum written by ichabod801 Validating User Input
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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