1. 程式人生 > >python 判斷手機號碼和正整數

python 判斷手機號碼和正整數

import re

'''
輸入手機號碼,判斷手機號碼是否為11位,是否為1開頭的數值
'''
def get_phone():
    while True:
        phone = input('請輸入手機號碼:')
        if len(phone) == 11 and re.match(r'1\d{10}', phone):
            return phone
        else:
            print('請輸入11位的手機號')

'''
輸入數量,判斷數量是否為正整數
'''
def get_amount():
    while True:
        amount = input('請輸入數量:')
        if amount.isdigit() and int(amount) >= 0:
            return amount
        else:
            print('請輸入正整數!')