x_and_o = { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', } def void(): print(''' ______________________________________________ | | | | | 1 | 2 | 3 | ______________________________________________ | | | | | 4 | 5 | 6 | ______________________________________________ | | | | | 7 | 8 | 9 | ______________________________________________ ''') def printer(): print(f''' ______________________________________________ | | | | | {x_and_o[1]} | {x_and_o[2]} | {x_and_o[3]} | ______________________________________________ | | | | | {x_and_o[4]} | {x_and_o[5]} | {x_and_o[6]} | ______________________________________________ | | | | | {x_and_o[7]} | {x_and_o[8]} | {x_and_o[9]} | ______________________________________________ ''') def command_x(): command = int(input('command X ')) if x_and_o[command] == f'{str(command)}' and x_and_o[command] != 'O': x_and_o[command] = 'X' return True elif x_and_o[command] != f'{str(command)}': print('Invalid') return False def command_o(): command = int(input('command O ')) if x_and_o[command] == f'{str(command)}' and x_and_o[command] != 'X': x_and_o[command] = 'O' return True elif x_and_o[command] != f'{str(command)}': print('Invalid') return False def tie(): for number in x_and_o: if number == 'O' or 'X': print('Tie') return True def winner_x(): if x_and_o[1] == 'X' and x_and_o[2] == 'X' and x_and_o[3] == 'X': print('Winner X') return True if x_and_o[4] == 'X' and x_and_o[5] == 'X' and x_and_o[6] == 'X': print('Winner X') return True if x_and_o[7] == 'X' and x_and_o[8] == 'X' and x_and_o[9] == 'X': print('Winner X') return True if x_and_o[1] == 'X' and x_and_o[4] == 'X' and x_and_o[7] == 'X': print('Winner X') return True if x_and_o[2] == 'X' and x_and_o[5] == 'X' and x_and_o[8] == 'X': print('Winner X') return True if x_and_o[3] == 'X' and x_and_o[6] == 'X' and x_and_o[9] == 'X': print('Winner X') return True if x_and_o[1] == 'X' and x_and_o[5] == 'X' and x_and_o[9] == 'X': print('Winner X') return True if x_and_o[3] == 'X' and x_and_o[5] == 'X' and x_and_o[7] == 'X': print('Winner X') return True else: return False def winner_o(): if x_and_o[1] == 'O' and x_and_o[2] == 'O' and x_and_o[3] == 'O': print('Winner O') return True if x_and_o[4] == 'O' and x_and_o[5] == 'O' and x_and_o[6] == 'O': print('Winner O') return True if x_and_o[7] == 'O' and x_and_o[8] == 'O' and x_and_o[9] == 'O': print('Winner O') return True if x_and_o[1] == 'O' and x_and_o[4] == 'O' and x_and_o[7] == 'O': print('Winner O') return True if x_and_o[2] == 'O' and x_and_o[5] == 'O' and x_and_o[8] == 'O': print('Winner O') return True if x_and_o[3] == 'O' and x_and_o[6] == 'O' and x_and_o[9] == 'O': print('Winner O') return True if x_and_o[1] == 'O' and x_and_o[5] == 'O' and x_and_o[9] == 'O': print('Winner O') return True if x_and_o[3] == 'O' and x_and_o[5] == 'O' and x_and_o[7] == 'O': print('Winner O') return True else: return False count = 1 void() while count < 9: while not command_x(): command_x() printer() count += 1 if winner_x() is True: break while not command_o(): command_o() printer() count += 1 if winner_o() is True: break if count >= 9: print('Tie')
Hi everyone, I am trying to make a simple Tic Tac Toe game but I am experiencing an issue. If the player does an illegal move like picking a value that is already picked before I want the program to print "invalid" and let the player choose another number on the table But here's the problem, If a player does an illegal move it prints "invalid" but then lets the player pick 2 values to assign instead of 1, if this doesn't explain the issue here's what happens in the executed: The issue:
Any idea what could be causing the issue? I am a beginner with python so I don't know much, I am using python 3.x
https://stackoverflow.com/questions/65837992/if-statement-allowing-user-to-to-give-2-inputs-when-theyre-only-allowed-to-give January 22, 2021 at 09:05AM
没有评论:
发表评论