Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to simply add a integer that the user types in, and put that into a register.
I am trying to check that what the user has typed in is in fact a integer and also 11 numbers long before adding it to a list (register).

Python
register = []

def add_number(number):
    if isinstance(number, int) and len(number) == 11:
        register.append(number)
        
add_number(raw_input("add a number to the register"))

print register


I have googled this and tried many ways to do this using things like type to check if it is a integer or converting the input into a string first then checking if that string matches 1234567890 but am confused why this wont work. I have got a function to work similar to this one but only with names instead so i can't understand why this wont work.
Posted

1 solution

Well, this is working just fine:
Python
register = []
 
def add_number(number):
    n = str(number)
    if n.isdigit() and len(n) == 11:
        register.append(number)
        
add_number(12345678901)
add_number(12345678902)
 
print register


Still, please note, that 1234567890 contains only 10 digits.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900