示例:python
#!/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()