我是初學者,正在撰寫一個接受用戶輸入的應用程式。我正在使用類,對于輸入驗證,我撰寫了下面的例外代碼。當我嘗試檢查無效輸入時,它不起作用。請有人告訴我該怎么做:)
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")
uj5u.com熱心網友回復:
請試試這個:
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()
我希望它能解決你的問題
uj5u.com熱心網友回復:
您也可以在建構式中呼叫 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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/346608.html
