Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if variable is numeric

Status
Not open for further replies.

Goofus828

Programmer
Nov 9, 2005
127
0
16
US

Hi all, on a scale of 1 to 100, 100 being a Python expert, I am a 1.
I inherited a bunch of Python scripts and now I have to change one.

This is the current code. It checks the length of the PropID and if it is 10, then it is a good ID.
Python:
if len(str(self.propid)) == 10:
    self.is_valid_sungard_id = True
else:
    self.is_valid_sungard_id = False
However the ID needs to start with a numeric for it to be truly valid.

Can anyone help?
Code:
if Left 1 is numeric and the length is 10 then it is a good PropID

Thanks in advance!
 
1st of all you want to check each condition separately (this will make it easier to maintain & add attitional check of modify existing ones.

the following is some very curde code that may need a little cleaning up

Code:
#new method to add to your class
def check_id(self):
[indent]valid_first_chars=[str(x) for x in range(10)]
id=str(self.propid) # isnt this a string anyway?
if len(id)!=0:
[indent]return False[/indent]
if id[0] not in valid_first_chars:
[indent]return False[/indent]
#add additional checks here if req
return True
[/indent]
#using the new method
self.is_valid_sungard_id=self.check_id(self)

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
if len(id) !=0:

Deliberate typo to check everyone was watching (thats my story & i am sticking to it :) )

str.isdigit()

I forgot about that, although my solution of checking members of a list does give some scope for amending the valid lead character

Hey I said it was quick & dirty code.

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top