Python 中用户输入列表的函数验证

Function validation for list of user inputs in Python

我是初学者,我正在编写一个接受用户输入的应用程序。我正在使用 class,对于输入验证,我为异常编写了下面的代码。当我尝试检查无效输入时,它不起作用。请有人告诉我该怎么做:)

class Game:

    def __init__(self, packs_purchase, dice_purchase, board_games_purchase):
        self.packs_purchase = packs_purchase
        self.dice_purchase = dice_purchase
        self.board_games_purchase = board_games_purchase
        

    def validate_inputs(self,packs_purchase, dice_purchase, board_games_purchase):
        all_inputs = zip(packs_purchase, dice_purchase, board_games_purchase)
        
        #validate length of inputs to be same
        if not len(packs_purchase) == len(dice_purchase) == len(board_games_purchase):
            raise ValueError("Invalid input- customer order size must be equal")

        for pack, dice, board in all_inputs:
            #validate each list has whole numerical input
            if not isinstance(pack, int) or not isinstance(dice, int) or not isinstance(board, int):
                raise TypeError("Invalid input- please check input must be integer")
            #validate all inputes to be positive integers
            if pack < 0 or dice < 0 or board < 0:
                raise Exception("Invalid input- all inputs must be positive integer")

请试试这个:

class Game:

    def __init__(self, packs_purchase, dice_purchase, board_games_purchase):
        self.packs_purchase = packs_purchase
        self.dice_purchase = dice_purchase
        self.board_games_purchase = board_games_purchase
        

    def validate_inputs(self):
        all_inputs = zip(self.packs_purchase, self.dice_purchase, self.board_games_purchase)
        
        #validate length of inputs to be same
        if not len(self.packs_purchase) == len(self.dice_purchase) == len(self.board_games_purchase):
            raise ValueError("Invalid input- customer order size must be equal")

        for pack, dice, board in all_inputs:
            #validate each list has whole numerical input
            if not isinstance(pack, int) or not isinstance(dice, int) or not isinstance(board, int):
                raise TypeError("Invalid input- please check input must be integer")
            #validate all inputes to be positive integers
            if pack < 0 or dice < 0 or board < 0:
                raise Exception("Invalid input- all inputs must be positive integer")

packs_purchase=["hi",10]
dice_puchase=[100,100]
board_games_purchase=[20,20]
x = Game(packs_purchase,dice_puchase,board_games_purchase)
x.validate_inputs()

希望能解决你的问题

您也可以在构造函数中调用 validate_inputs() 方法。这将在实例化对象时进行检查:

class Game:

    def __init__(self, packs_purchase, dice_purchase, board_games_purchase):
        self.packs_purchase = packs_purchase
        self.dice_purchase = dice_purchase
        self.board_games_purchase = board_games_purchase
        self.validate_inputs()
        

    def validate_inputs(self):
        all_inputs = zip(self.packs_purchase, self.dice_purchase, self.board_games_purchase)
        
        #validate length of inputs to be same
        if not len(self.packs_purchase) == len(self.dice_purchase) == len(self.board_games_purchase):
            raise ValueError("Invalid input- customer order size must be equal")

        for pack, dice, board in all_inputs:
            #validate each list has whole numerical input
            if not isinstance(pack, int) or not isinstance(dice, int) or not isinstance(board, int):
                raise TypeError("Invalid input- please check input must be integer")
            #validate all inputes to be positive integers
            if pack < 0 or dice < 0 or board < 0:
                raise Exception("Invalid input- all inputs must be positive integer")