473,289 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

checking the validation of input value

Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self_.A = 0
  4.           self._A = 0
  5.  
  6.  
  7.       def setA(self, A)
  8.            return self._A = A
  9. ....
  10. #when I run program
  11. >>Cal1 = calculation()
  12. # I would like to have an error message: "invalid" if I enter: 
  13. >>Cal1.setA()
  14. >>"Invalid"
  15.  
  16. #How can I do this?
  17.  
  18.  
Oct 16 '07 #1
3 1643
elcron
43
Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self_.A = 0
  4.           self._A = 0
  5.  
  6.  
  7.       def setA(self, A)
  8.            return self._A = A
  9. ....
  10. #when I run program
  11. >>Cal1 = calculation()
  12. # I would like to have an error message: "invalid" if I enter: 
  13. >>Cal1.setA()
  14. >>"Invalid"
  15.  
  16. #How can I do this?
  17.  
  18.  
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.
Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self._A = 0
  4.       def setA(self, A=False):
  5.           invalid = [13,15,11] # values you don't want to accept
  6.           isValid =  (A and                              # ignores A if False or None
  7.                       not A in invalid and               # ignores A if in a list of unwanted variables
  8.                       not type(A) == type('String') and  # ignores A based on a string
  9.                       0 < A < 30                         # ignores A if not within a range of numbers
  10.                       )
  11.           if not isValid:
  12.               print A, 'is invalid'
  13.               return False
  14.           self._A = A
  15.           return True
  16.  
  17. a = calculation()
  18.  
  19. print 'trying to ignore passing in A'
  20. a.setA()
  21. print a._A
  22.  
  23. print 'trying to set A as 13'
  24. a.setA(13)
  25. print a._A
  26.  
  27. print 'trying to set A as "A String"'
  28. a.setA("A String")
  29. print a._A
  30.  
  31. print 'trying to set A as -5'
  32. a.setA(-5)
  33. print a._A
  34.  
  35. print 'trying to set A as 35'
  36. a.setA(35)
  37. print a._A
  38.  
  39. ##########output##########
  40. #trying to ignore passing in A
  41. #False is invalid
  42. #0
  43. #trying to set A as 13
  44. #13 is invalid
  45. #0
  46. #trying to set A as "A String"
  47. #A String is invalid
  48. #0
  49. #trying to set A as -5
  50. #-5 is invalid
  51. #0
  52. #trying to set A as 35
  53. #35 is invalid
  54. #0
  55. #trying to set A as 8
  56. #8
  57.  
Oct 16 '07 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self_.A = 0
  4.           self._A = 0
  5.  
  6.  
  7.       def setA(self, A)
  8.            return self._A = A
  9. ....
  10. #when I run program
  11. >>Cal1 = calculation()
  12. # I would like to have an error message: "invalid" if I enter: 
  13. >>Cal1.setA()
  14. >>"Invalid"
  15.  
  16. #How can I do this?
  17.  
  18.  
Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self_.A = 0
  4.           self._A = 0
  5.  
  6.  
  7.         def setA(self, *A) # '*' says: use a variable length list of arguments
  8.             if not A:
  9.                 print "invalid"
  10.                 return
  11.             self._A = A[0]
  12.  
But I think that we already showed you that.
Oct 16 '07 #3
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. class calculation:
  2.       def __init__(self):
  3.           self_.A = 0
  4.           self._A = 0
  5.  
  6.  
  7.         def setA(self, *A) # '*' says: use a variable length list of arguments
  8.             if not A:
  9.                 print "invalid"
  10.                 return
  11.             self._A = A[0]
  12.  
But I think that we already showed you that.
Yep. Post #16 in this thread.
Oct 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these...
3
by: CJM | last post by:
I'm adding some extra features to an intranet-based ASP application...(IE6 clients) As part of the process, I thought I would give the html a mid-life upgrade, ie. remove much of the tag-soup,...
5
by: EviL KerneL | last post by:
Hi - I am trying to figure out a way to enforce the validation included for this form based on whether the user chooses "email" or "phone" as the contact choice. Right now it is set to enforce...
2
by: Dnna | last post by:
I have a table which is bound to an Internet Explorer XML data island. I'm using ASP.NET's client-side validators for an input field in the table. The problem is that if the input fields are in...
2
by: daniel.boorn | last post by:
Form validation using JavaScript has never been as easy and simple! We have developed a free generic form validation script that can validate any form with very little JavaScript required in form!...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
4
by: yatin | last post by:
hi, friend. i have a problem in image validation plz check it. i have to include a image validation in foam 1. but i sending a details through form2. But when i include a image validation in foam1,...
1
by: lilbit02 | last post by:
Hi, I have a form that utilizes validation as most do via javascript. This form worked totaly fine until I needed to add a dynamic div now the validation doesn't work. It does work for the first...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.