Vida Maleki
Vida Maleki

Reputation: 3

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

Upvotes: 0

Views: 111

Answers (2)

intedgar
intedgar

Reputation: 681

You could call the validate_inputs() method in the constructor as well. 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")

Upvotes: 0

saeedhosseini
saeedhosseini

Reputation: 82

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

Upvotes: 1

Related Questions