0

I am making a connect4 game on python, and when a column is full, this error is raised when a player puts a piece in the full column. How do I make a if statement to say that if this error is raised, print("Make a valid move?")

class ConnectFourGameOverError(Exception):
    '''Raised whenever an attempt is made to make a move after the game is
    already over'''
    pass
4

1 回答 1

2

利用try/except

try:
    # your code here
except YourError:
    # do something

它的作用是执行一段代码,并“捕获”给定的异常(如果发生)。例如,检查以下代码:

try:
    num = raw_input('Enter an number: ')  # user enters something
    num = int(num)  # Python tries to convert it to an integer
except ValueError:  # if the input was invalid
    print 'You didn\'t enter an valid number'
于 2013-02-04T09:02:25.350 回答