Python Forum
need help with making a validating function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help with making a validating function
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
I gave you the two tests for that. All you have to do is return the and of those two tests.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I am curious whether in current context this is expected or unexpected:

>>> num = '000000001'
>>> len(num)
9
>>> num.isnumeric()
True
>>> int(num)
1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(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²'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
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.
Reply
#9
I blame approaching Friday....

def validate(test):
    return (len(test) == 9
            and  
            test[0] != '0'
            and
            all(num in '0123456789' for num in test))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] formula for validating monetary values? kakos_k9 1 754 Dec-17-2022, 09:28 PM
Last Post: woooee
  Making a function more efficient CatorCanulis 9 1,830 Oct-06-2022, 07:47 AM
Last Post: DPaul
Question Making a copy list in a function RuyCab 1 1,794 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Making a code.py file to function, does not run hobbyist 6 2,899 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Validating user input WJSwan 2 2,121 Jul-06-2020, 07:21 AM
Last Post: menator01
  making a function that writes variables (is possible?) miker2808 3 2,342 Jan-30-2020, 06:27 PM
Last Post: buran
  Validating the functionality of the application rpalakodety 1 1,768 Dec-30-2019, 07:58 PM
Last Post: ndc85430
  Validating information from .csv file before executemany mzmingle 7 4,432 Apr-15-2019, 01:40 PM
Last Post: mzmingle
  Base for making a search function SheeppOSU 1 2,301 Mar-19-2019, 11:37 PM
Last Post: Yoriz
  Making list empty after return in function dan789 10 5,751 Nov-24-2018, 11:54 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020