[Python] How do you make sure that the user inputs the correct type (list in my case) when...

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to check the inputs when a new class object is created to determine if they are input correctly and if say "theMoves" is input incorrectly then do something like ignore it or attempt at converting it to a list.

    Class Card:
    def __init__(self, theName,theHP, theMoves ):
    self.theName=str(theName)
    self.theHp=int(theHP)
    self.theMoves=theMoves # [(),()..]


    Class Deck:
    #more code here
    #When i try adding a card to the deck i get an index error because theMoves in the Card class arnt correct. It works with the c0,c1

    def main():
    #c0=Card("Dave",454,[("Fieball",999)]) # works
    c1=Card("Bob",500,[("Fireball",999),("Flame",999),("Waterblast",499)]) #works
    #c2=Card("Peter",400,(fire,342)) # Fix
    #c3=Card("Josh",300,waterb,22) #fix (maybe by just ignoring the moves after checking each varible)


    I'm wondering if there is a way so that if someone enters the information incorrect like I have done in "c2"/"c3" then it should either convert the values to match the format like c0 or c1 or just ignore the value all together. If its easier, I don't mind just ignore theMove value if entered incorrectly but I'm not sure how to go about doing it? When I looked online I seen someone mention the new method but I'm not too sure with python and objects on how I would go about doing it.

    Continue reading...

Compartilhe esta Página