Python程序中program方法的while循环无法终止问题求助
Python程序循环终止问题及正则验证错误修复建议
我正在开发一款支持添加、删除、列出数据的Python程序,当前遇到program方法内的while循环无法终止的问题。即使在addRecord函数中输入姓名,程序仍会输出“pls enter a valid number again ”。该问题在编写addRecord函数之前就已存在,附上相关代码,寻求解决建议或问题原因解释。
import re #list of user information list_id = [] list_name = [] list_surname = [] list_age = [] list_nationalid = [] list_mail = [] class Record: def __init__(self,programName): self.programName = programName self.loop = True print("welcome {}.".format(programName)) def program(self): while True: #the loop gonna keep ask if input is not a correct number try: userInput = int(input ("what you want to do?\n1-addRecord\n2-removeRecord\n3-listRecord\n4-exitRecord\n")) if userInput < 5 and userInput > 0: self.menu(userInput) break else: print("pls enter a valid number") except Exception : print("pls enter a valid number again ") def menu(self,userInput): self.userInput = userInput if userInput == 1: print("addRecord is opening") self.addRecord() if userInput == 2: print("removeRecord is opening") self.removeRecord() if userInput == 3: print("list Record are opening") self.listRecord() if userInput == 4: self.exitRecord() def addRecord(self): while True: user_name = input("pls enter your name") if user_name == re.findall("[^a-z]"): list_name.append(user_name) print ("addRecord_name worked") break else: print ("enter a valid name") while True: user_surname = input("pls enter your name") if user_surname == re.findall("[^a-zA-E]"): list_surname.append(user_surname) print ("addRecord_surname worked") break else: print ("enter a valid surname") while True: user_age = input("pls enter your age") if user_age == re.findall("[^0-9]"): list_age.append(user_age) print ("addRecord_age worked") break else: print ("enter a valid age") while True: user_nationalid = input("pls enter your national id") if user_name == re.findall("[^0-9]"): list_nationalid.append(user_nationalid) print ("addRecord_nationalid worked") break else: print ("enter a valid national id") while True: user_mail = input("pls enter your name") if user_name == re.search("@" and ".com" , user_mail): list_mail.append(user_mail) print ("addRecord_mail worked") break else: print ("enter a valid national id") def removeRecord(self): pass def listRecord(self): pass def backToMenu(self): pass def exitRecord(self): pass System = Record("admin") System.program() #its for keep working `program`
问题原因及修复建议
1. program方法循环终止问题
原代码中program方法的while True循环在调用self.menu(userInput)后执行了break,这会导致执行完一次操作后直接退出主循环,无法回到主菜单继续操作。如果期望执行完操作后返回主菜单,需要删除这个break语句,修改后的program方法如下:
def program(self): while True: try: userInput = int(input ("what you want to do?\n1-addRecord\n2-removeRecord\n3-listRecord\n4-exitRecord\n")) if userInput < 5 and userInput > 0: self.menu(userInput) # 移除break,执行完操作后回到循环开头,重新显示主菜单 else: print("pls enter a valid number") except Exception : print("pls enter a valid number again ")
同时,exitRecord方法需要实现终止循环的逻辑,比如:
def exitRecord(self): print("Exiting program...") exit()
2. addRecord方法中正则验证的多处错误
这是导致输入姓名后一直提示错误的核心原因,具体问题及修复:
- 正则使用错误:
re.findall需要传入正则表达式和待匹配的字符串,且正则规则写反。比如验证姓名应该匹配全小写字母,而不是非小写字母,建议用re.fullmatch判断整个字符串是否符合规则。 - 变量混淆:验证身份证号时错误使用
user_name进行判断,应该用user_nationalid。 - 输入提示错误:输入姓氏时提示“pls enter your name”,邮箱输入提示也是错误的,需要修正。
- 邮箱验证逻辑错误:
re.search("@" and ".com" , user_mail)逻辑错误,且错误地用user_name判断匹配结果,应该用正则匹配合法邮箱格式。
修改后的addRecord方法示例:
def addRecord(self): # 验证姓名:仅允许小写字母 while True: user_name = input("pls enter your name: ") if re.fullmatch(r'[a-z]+', user_name): list_name.append(user_name) print("addRecord_name worked") break else: print("enter a valid name (only lowercase letters)") # 验证姓氏:允许大小写A-E的字母 while True: user_surname = input("pls enter your surname: ") if re.fullmatch(r'[a-zA-E]+', user_surname): list_surname.append(user_surname) print("addRecord_surname worked") break else: print("enter a valid surname (only letters A-E, case insensitive)") # 验证年龄:仅允许数字 while True: user_age = input("pls enter your age: ") if re.fullmatch(r'[0-9]+', user_age): list_age.append(user_age) print("addRecord_age worked") break else: print("enter a valid age (only numbers)") # 验证身份证号:仅允许数字 while True: user_nationalid = input("pls enter your national id: ") if re.fullmatch(r'[0-9]+', user_nationalid): list_nationalid.append(user_nationalid) print("addRecord_nationalid worked") break else: print("enter a valid national id (only numbers)") # 验证邮箱:包含@和.com while True: user_mail = input("pls enter your mail: ") if re.fullmatch(r'.+@.+\.com', user_mail): list_mail.append(user_mail) print("addRecord_mail worked") break else: print("enter a valid mail (must contain @ and .com)")
其他优化建议
- 建议用一个字典或类实例来存储用户信息,而不是多个独立的列表,更便于管理。
- 异常处理尽量精确,比如只捕获
ValueError(输入无法转为整数的情况),而不是捕获所有Exception。
内容的提问来源于stack exchange,提问作者fethullah cücü
相关产品推荐
相关产品推荐

