Call More
Call More

Reputation: 43

How to limit the type of characters input python

How can I limit the number of characters and type of characters that can be inputted.

s = input('What is your number)

a number can only be 9 digits so it should be limited to 9 digits and only numbers

x = input('what is your email address)

an email address can contain a minimum of 8 characters and a maximum of 60 characters and also must contain an @ symbol.

How would I go about doing this?

Upvotes: 3

Views: 1215

Answers (1)

Mark White
Mark White

Reputation: 660

In fact, my dear friend, you cannot limit input function, but you can achieve what you want by this way:

def isnumber(s):
    if s.isdigit() and len(s) <= 9:
        return True
    else:
        print('you should input number and length less than 9')
        return False

def main():
    s = input('What is your number')
    while not isnumber(s):
        s = input('What is your number')
    print("your number is", s)


if __name__ == "__main__":
    main()

Upvotes: 2

Related Questions