Basic Python Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xstrain
    New Member
    • Mar 2008
    • 1

    Basic Python Validation

    Having difficulty creating error handling when inputting an integer

    dvdQuant = input("Enter quantity of DVD: ")

    how can I get python to return a print "Error Message" if an integer is not entered instead of
    Enter quantity of DVD: gyfj
    Traceback (most recent call last):
    File "I:/Python/CourseworkVer3. py", line 203, in <module>
    addDvd(catalogu e)
    File "I:/Python/CourseworkVer3. py", line 129, in addDvd
    dvdQuant = input("Enter quantity of DVD: ")
    File "<string>", line 1, in <module>
    NameError: name 'gyfj' is not defined

    I have the input put into a dictionary so I can multiply with another integer in another function, if I change input to raw_input then when I try and multiply it doesn't recognise the input as an integer

    Ihope this isn't too vague and any help would be appreciated
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    The quickest way to do this is to use raw_input to get a string, then loop over each character in said string and see if it is between '0' and '9'. If you clear the loop without any issues, you have an integer.

    Comment

    • elcron
      New Member
      • Sep 2007
      • 43

      #3
      Use try except to handle errors.

      [code=python]
      try:
      n = int( raw_input("Ente r a number: ") )
      print "n=%s, n squared=%s"%(n, n*n)
      except ValueError:
      print "Invalid input."
      [/code]

      Comment

      • Subsciber123
        New Member
        • Nov 2006
        • 87

        #4
        [CODE=python]n=raw_input("Nu mber: ")
        while not n.isdigit(): # this is a builtin method of all str objects
        print "Error: NaN"
        n=raw_input("Nu mber: ")
        n=int(n)
        [/CODE]

        I believe that this is the simplest possible way to do it. Looping through the string is tedious and processor intensive, even for short strings. Although this is almost guaranteed to be a short string, you should get into the habit of creating readable and efficient code.

        Comment

        • marcial
          New Member
          • Mar 2008
          • 2

          #5
          Im also new with python, and what would I have to do if I want the script or the section to restart every time the user inputs wrong information??

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by marcial
            Im also new with python, and what would I have to do if I want the script or the section to restart every time the user inputs wrong information??
            Following is an example using a while loop.
            [code=Python]while True:
            s = raw_input("Ente r an integer")
            try:
            i = int(s)
            break
            except ValueError:
            print 'Invalid input'

            print 'Your input of %d is an integer.' % i[/code]

            Comment

            • marcial
              New Member
              • Mar 2008
              • 2

              #7
              Originally posted by bvdet
              Following is an example using a while loop.
              [code=Python]while True:
              s = raw_input("Ente r an integer")
              try:
              i = int(s)
              break
              except ValueError:
              print 'Invalid input'

              print 'Your input of %d is an integer.' % i[/code]
              Thanks! I tried to use try-except before without the while loop, and it seems to work very weird, but now with while it works perfect! Thanks!

              Comment

              • fatroshi
                New Member
                • Dec 2009
                • 12

                #8
                Works great for me 2 :)

                Comment

                Working...