简体   繁体   English

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

[英]Function validation for list of user inputs in Python

I am a beginner, and I am writing an application that accepts user input.我是初学者,正在编写一个接受用户输入的应用程序。 I am using class, and for input validation, I wrote the code below for exceptions.我正在使用类,对于输入验证,我编写了下面的异常代码。 When I am trying to check invalid inputs, it's not working.当我尝试检查无效输入时,它不起作用。 please, someone show me what to do :)请有人告诉我该怎么做:)

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")

Please try this:请试试这个:

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()

I hope it solves your problem我希望它能解决你的问题

You could call the validate_inputs() method in the constructor as well.您也可以在构造函数中调用 validate_inputs() 方法。 This would already check when instantiating the object:这将在实例化对象时进行检查:

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM