# 在 python 的 connect 4 游戏中验证输入

[英]Validating input in a connect 4 game in python

``````board = [['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['⚪', '⚪', '⚪', '⚪', '⚪', '⚪', '⚪'],
['1️⃣ ', '2️⃣ ', '3️⃣ ', '4️⃣ ', '5️⃣ ', '6️⃣ ', '7️⃣']]

ROWS = 7
COLUMNS = 7

def print_board():
'''
Prints out the game board
'''
for row in range(0, ROWS):
for col in range(0, COLUMNS):
print(board[row][col], end=' ')
print(" ")

def place_chip(col, player):
'''
Places chip in the first empty slot from the bottom in a column
'''
col = col - 1
for rows in range(ROWS-1, -1, -1):
if board[rows][col] == '⚪':
board[rows][col] = player
break

def validate_input(x):
while True:
if x < 1:
x = int(input(f'Column {x} does not exist. Please choose column 1-7: '))
return x
elif x > 7:
x = int(input(f'Column {x} does not exist. Please choose column 1-7: '))
return x
else:
break
return x

def play_game():
print_board()
x = int(input('Player 1 select a column(1-7): '))
validate_input(x)
place_chip(validate_input(x), '🔴')
print_board()

play_game()
``````

© 2020-2024 STACKOOM.COM