lundi 24 août 2020

Python If Statement is executed, but nothing occurs [closed]

I'm having trouble understanding where I might be screwing up my If Statement.

In this function I send an object reference called current_player that holds an object, either player1 or player2. It then prompts for input (A2, B1, C3, etc), processes said input as the current_player's 'choice' so that it can be parsed for grid_data, makes sure that choice is valid, updates the grid if so, then will print said grid.

However as it gets to the point where I make sure the player's choice is valid (the series of if > elif > elif > else), nothing happens at all. It seems to completely skips over the entire block of code. I figure that if the if and elifs fail, else should still function.

Where am I screwing up here?

def grid_update(current_player):
# Collect current_player, prompt for input, process input as their 'choice', update grid with their 'choice', print

gd = GameData.grid_data
draw = GameData.draw
prompt = "Prompt"

# List of acceptable alpha and numeric values for grid
grid_values = ['a', 'b', 'c', 1, 2, 3]

print(f"{current_player.name}, please place your {current_player.gamepiece}! ")
draw(prompt)
current_player.input = input()

# Check input for special exit command
if current_player.input == "ABORT":
    print("Aborting")
    return

# Convert player input into a list of two values (1 alpha, 1 num) to be used for placing their piece onto the grid
for grid_point in current_player.input:
    if grid_point.isdigit():
        current_player.choice.append(int(grid_point))
    else:
        current_player.choice.append(grid_point.lower())

grid_key_value1 = current_player.choice[0]
grid_key_value2 = current_player.choice[1]

grid_key_intersection = gd[grid_key_value1][grid_key_value2]

# Error tracking
print("current_player.input ", current_player.input)
print("current_player.choice ", current_player.choice)
print("grid_key_value1: ", grid_key_value1)
print("grid_key_value2: ", grid_key_value2)
print("grid_key_intersection ", grid_key_intersection)

# Clear choice and input from current player
current_player.choice = []
current_player.input = None

# Check to make sure that the player's choice isn't invalid or an already occupied position on the grid
if grid_key_intersection == '_':
    grid_key_intersection = current_player.gamepiece
elif grid_key_value1 not in grid_values:
    print(f"You can't place that there! Please select another location for your {current_player.gamepiece}:")
    grid_update(current_player)
elif grid_key_value2 not in grid_values:
    print(f"You can't place that there! Please select another location for your {current_player.gamepiece}:")
    grid_update(current_player)
else:
    print(f"You can't place that there! Please select another location for your {current_player.gamepiece}:")
    grid_update(current_player)

print("Somehow did not enter if loop")

Aucun commentaire:

Enregistrer un commentaire