Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation checking
#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


Messages In This Thread
Validation checking - by Help_me_Please - Aug-14-2019, 01:31 PM
RE: Validation checking - by buran - Aug-14-2019, 01:35 PM
RE: Validation checking - by perfringo - Aug-14-2019, 01:39 PM
RE: Validation checking - by Help_me_Please - Aug-14-2019, 01:43 PM
RE: Validation checking - by perfringo - Aug-14-2019, 01:52 PM
RE: Validation checking - by Help_me_Please - Aug-14-2019, 03:41 PM
RE: Validation checking - by perfringo - Aug-14-2019, 07:55 PM
RE: Validation checking - by ichabod801 - Aug-14-2019, 05:01 PM
RE: Validation checking - by jefsummers - Aug-14-2019, 06:39 PM
RE: Validation checking - by Help_me_Please - Aug-15-2019, 02:17 PM
RE: Validation checking - by ichabod801 - Aug-15-2019, 03:06 PM
RE: Validation checking - by Help_me_Please - Aug-15-2019, 03:33 PM
RE: Validation checking - by ichabod801 - Aug-15-2019, 03:36 PM
RE: Validation checking - by Help_me_Please - Aug-15-2019, 04:03 PM
RE: Validation checking - by buran - Aug-15-2019, 04:51 PM
RE: Validation checking - by ichabod801 - Aug-15-2019, 04:54 PM
RE: Validation checking - by Help_me_Please - Aug-16-2019, 09:40 AM
RE: Validation checking - by perfringo - Aug-16-2019, 10:17 AM
RE: Validation checking - by jefsummers - Aug-16-2019, 11:46 AM
RE: Validation checking - by perfringo - Aug-16-2019, 12:11 PM
RE: Validation checking - by ichabod801 - Aug-16-2019, 12:33 PM
RE: Validation checking - by Help_me_Please - Aug-16-2019, 12:46 PM
RE: Validation checking - by jefsummers - Aug-16-2019, 02:18 PM
RE: Help please stuck with summer work - by nilamo - Aug-15-2019, 04:47 PM
Connect 4 - by Help_me_Please - Aug-16-2019, 11:38 AM

Forum Jump:

User Panel Messages

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