Home > Enterprise >  Is this variable just a string of numbers?
Is this variable just a string of numbers?

Time:02-14

I want a code to specify which variables in a dictionary like the following are not just a string of numbers?

myDictionary = {
'first' : '2345687554',
'second' : 'fgdjsa87gh',
'third' : '87hfkd77d'
}

In fact, I wrote a function that takes a number of usernames and passwords as arguments and returns the names that qualify in a list:

def check_registration_rules (**x):   
mylist = []
for usname in x.keys():
    if (len(usname) >= 4) and (usname != 'quera' and usname != 'codecup') and (len(x[usname]) >= 6 ):
        mylist.append(usname)
return mylist

But it is necessary to find out whether the password of a username is just a string of numbers or not. If it is a string of numbers, it does not have the necessary conditions. I did not find a solution to this

CodePudding user response:

In Python you can check if a string is made out of only digits with str.isdigit().

>>> "42".isdigit()
True
>>> "53SS0".isdigit()
False

In your case something like this should work:

if (not usname.isdigit()):
    ...
  • Related