checking the validation of input value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • python101
    New Member
    • Sep 2007
    • 90

    #1

    checking the validation of input value

    [code=python]
    class calculation:
    def __init__(self):
    self_.A = 0
    self._A = 0


    def setA(self, A)
    return self._A = A
    ....
    #when I run program
    >>Cal1 = calculation()
    # I would like to have an error message: "invalid" if I enter:
    >>Cal1.setA()
    >>"Invalid"

    #How can I do this?

    [/code]
  • elcron
    New Member
    • Sep 2007
    • 43

    #2
    Originally posted by python101
    [code=python]
    class calculation:
    def __init__(self):
    self_.A = 0
    self._A = 0


    def setA(self, A)
    return self._A = A
    ....
    #when I run program
    >>Cal1 = calculation()
    # I would like to have an error message: "invalid" if I enter:
    >>Cal1.setA()
    >>"Invalid"

    #How can I do this?

    [/code]
    Here's some code that shows you how to require input and limit it in a number of ways including if nothing is passed in. A boolean is returned so you can check if it's a success or not.
    [code=python]
    class calculation:
    def __init__(self):
    self._A = 0
    def setA(self, A=False):
    invalid = [13,15,11] # values you don't want to accept
    isValid = (A and # ignores A if False or None
    not A in invalid and # ignores A if in a list of unwanted variables
    not type(A) == type('String') and # ignores A based on a string
    0 < A < 30 # ignores A if not within a range of numbers
    )
    if not isValid:
    print A, 'is invalid'
    return False
    self._A = A
    return True

    a = calculation()

    print 'trying to ignore passing in A'
    a.setA()
    print a._A

    print 'trying to set A as 13'
    a.setA(13)
    print a._A

    print 'trying to set A as "A String"'
    a.setA("A String")
    print a._A

    print 'trying to set A as -5'
    a.setA(-5)
    print a._A

    print 'trying to set A as 35'
    a.setA(35)
    print a._A

    ##########outpu t##########
    #trying to ignore passing in A
    #False is invalid
    #0
    #trying to set A as 13
    #13 is invalid
    #0
    #trying to set A as "A String"
    #A String is invalid
    #0
    #trying to set A as -5
    #-5 is invalid
    #0
    #trying to set A as 35
    #35 is invalid
    #0
    #trying to set A as 8
    #8
    [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by python101
      [code=python]
      class calculation:
      def __init__(self):
      self_.A = 0
      self._A = 0


      def setA(self, A)
      return self._A = A
      ....
      #when I run program
      >>Cal1 = calculation()
      # I would like to have an error message: "invalid" if I enter:
      >>Cal1.setA()
      >>"Invalid"

      #How can I do this?

      [/code]
      [code=python]
      class calculation:
      def __init__(self):
      self_.A = 0
      self._A = 0


      def setA(self, *A) # '*' says: use a variable length list of arguments
      if not A:
      print "invalid"
      return
      self._A = A[0]
      [/code]
      But I think that we already showed you that.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        [code=python]
        class calculation:
        def __init__(self):
        self_.A = 0
        self._A = 0


        def setA(self, *A) # '*' says: use a variable length list of arguments
        if not A:
        print "invalid"
        return
        self._A = A[0]
        [/code]
        But I think that we already showed you that.
        Yep. Post #16 in this thread.

        Comment

        Working...