请帮我用pyhton编写程序,分别输入3个字符串,依次验证其是否为有效的电话号码、邮政编码和网站网址格式。 (1)电话号码必须是8位号码,如果有区号,区号必须是3位。 (2)邮政编码必须是6位数字。 (3)网站网址的正则表示参考:r'^https?://\w+(?:.[^.]+)+(?:/.+)*$'
时间: 2023-05-30 18:04:55 浏览: 319
import re
# 验证电话号码
def check_phone_number(phone_number):
pattern = r'^\d{8}$|^\d{3}\-\d{8}$'
if re.match(pattern, phone_number):
return True
else:
return False
# 验证邮政编码
def check_postal_code(postal_code):
pattern = r'^\d{6}$'
if re.match(pattern, postal_code):
return True
else:
return False
# 验证网站网址
def check_website(website):
pattern = r'^https?://\w+(?:\.[^.\s]+)*(?:/\S+)*$'
if re.match(pattern, website):
return True
else:
return False
# 输入电话号码、邮政编码、网站网址
phone_number = input("请输入电话号码:")
postal_code = input("请输入邮政编码:")
website = input("请输入网站网址:")
# 验证并输出结果
if check_phone_number(phone_number):
print("电话号码是有效的。")
else:
print("电话号码不是有效的。")
if check_postal_code(postal_code):
print("邮政编码是有效的。")
else:
print("邮政编码不是有效的。")
if check_website(website):
print("网站网址是有效的。")
else:
print("网站网址不是有效的。")