判断re模块的布尔值
示例:
#!/usr/bin/python # coding:utf-8 # Author:Guido's admirers import re import time class card(object): def __init__(self): self.name = input("请输入姓名:\n") self.QQ = input("请输入QQ号:\n") self.phone = input("请输入电话号码:\n") self.home = input("请输入家庭住址:\n") def __str__(self): return "您的QQ格式有误,请从新输入!" def print(self): for i in range(3): print("系统正在打印.....") time.sleep(1) body = """ ====================================== 姓名:%s QQ:%s phone:%s 家庭住址:%s ====================================== """%(self.name, self.QQ, self.phone, self.home) print(body) def if_if(self): if bool(re.findall(r"^\d{5,12}@qq.com$", self.QQ)): #re模块返回值本身没有bool值,可用bool()转变bool类型。 if bool(re.findall(r"\b\d{11}\b", self.phone)): obj.print() else: print("您的手机号有误,请从新输入!") else: print(obj) card().if_if() #这里如果用对象obj则不会自动执行__init__方法 if __name__ == '__main__' : obj = card() obj.if_if()