Aug-15-2019, 04:47 PM
(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:This function perhaps is tricking you into thinking you're changing thedef 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
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 TrueAnd 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)