Python Forum

Full Version: need help with making a validating function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i am trying to write a program that checks the entered number and print if it valid the number also has to limited to 9 characters and has to be a callable function

atempt = 0  
  
num = ""


def validnum():  
    num = input(" ") 
    while atempt != 0:
        num = input(" ") 

        if num == num[0:9].isdigit: 
    
            print('valid num') 
    else:        
        print ('Invalid num')
validnum()
isdigit returns True if the string is all digits, but only if you call it (num.isdigit()). It's not clear exactly what you are trying to do, but if you need to see that the number is 9 digits long, you want len(num) == 9. Your atempt variable is just going to cause errors, because you never define atempt anywhere and then try to test it. I would just do a while True: loop, and break when the number is valid. I would also indent that else clause to tie it to the if, not the while.
i actually just want the function to tell me true if the number is 9 digits long and false otherwise that was like the main purpose of the function
I gave you the two tests for that. All you have to do is return the and of those two tests.
I am curious whether in current context this is expected or unexpected:

>>> num = '000000001'
>>> len(num)
9
>>> num.isnumeric()
True
>>> int(num)
1
If you want to have integers, just convert the input in a try-block and let the int() function raise a ValueError. Catch this ValueError and continue. If there was no ValueError, you got your result. In this case the else-block is executed and you can break out of the while true loop.

No matter what you put into the int() function, the function converts only valid integer.
If it's not valid, the function raises an ValueError.
(Mar-28-2019, 08:31 AM)DeaD_EyE Wrote: [ -> ]If you want to have integers, just convert the input in a try-block and let the int() function raise a ValueError. Catch this ValueError and continue. If there was no ValueError, you got your result. In this case the else-block is executed and you can break out of the while true loop.

No matter what you put into the int() function, the function converts only valid integer.
If it's not valid, the function raises an ValueError.

My line of thought was more towards this: if you validate input with string length and checking whether all characters in string are numeric then inputs starting with zero(s) will pass. If you later need to perform calculations with integer of specific length it may be a problem (instead of hundred-millions you may have 1), if it will be kept as string it doesn't matter.

str.isnumeric/isdigit validation has also this behaviour which must be also considered while taking user input:

>>> user_input = '1234567890²'
>>> len(user_input)
11
>>> user_input.isnumeric()
True
>>> user_input.isdigit()
True
>>> int(user_input)
/../
ValueError: invalid literal for int() with base 10: '1234567890²'
def validation(number):
if number.isnumeric() and int(number) > 0 and int(number) < 1000000000 :
return True
else :
return False

value=input("Please enter any number : ")
print(validation(value))


unable to add proper tabbing int the quote.
I blame approaching Friday....

def validate(test):
    return (len(test) == 9
            and  
            test[0] != '0'
            and
            all(num in '0123456789' for num in test))