python - Prompting the user for correct input - tic tac toe game -
i'm working on program simulates tic-tac-toe game between 2 players. program want far, except part ask players input. want program check whether (3 x 3) grid contains mark (for example, 'x' or 'o'), , keep prompting same player correct input before switching turns. here have far:
board = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] def main(): print_instructions() start = str(input("your game begin. press 'q' if want quit, or 'k' proceed: ")) while start != "q": get_input1() get_input2() def display_board(board): print(board[0], "|" , board[1] , "|" , board[2]) print("----------") print(board[3] , "|" , board[4] , "|" , board[5]) print("----------") print(board[6] , "|" , board[7] , "|" , board[8]) def print_instructions(): print("please use following cell numbers make move: ") display_board(board) def get_input1(): userinput = input("the first player marks 'x'. type integer between 0 8 indiciate want move: ") userinput = int(userinput) if userinput < 0 or userinput > 8 or board[userinput] == "x" or board[userinput] == "o": print("wrong input! cannot move there! ") else: board[userinput] = "x" display_board(board) def get_input2(): userinput = input("turn of second player. mark 'o'. move? type integer between 0 , 8: ") userinput = int(userinput) if userinput < 0 or userinput > 8 or board[userinput] == "x" or board[userinput] == "o": print("wrong input! cannot move there! ") else: board[userinput] = "o" display_board(board) main()
i still have write part program decides winner is, want def get_input
function right first. how can check valid input in case? tried using while
loop, 1 kept prompting user indefinitely without terminating (maybe did wrong there). appreciated.
maybe forgot take input in while loop! try this.
while userinput < 0 or userinput > 8 or board[userinput] == "x" or board[userinput] == "o": print("wrong input! cannot move there! ") userinput = input("please give valid move.") board[userinput] = "x"
Comments
Post a Comment