I want the user to select a number 1-7 and then check if that this is true, if not then they input a number again.
I am able to check this but if a wrong number is chosen it will ittirate the process 3 times even if a correct value is entered.. I have tried many different solutions to no avail. I just want to check the value until its right and then return the correct value to the game.
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()
Another approach, for a console game, is that the loop iterates, instead of three times, until a correct number is obtained, between 1 or 7 or the user indicates -1 (for example) in case he wants to end game!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.