Python登录注册作业报错求助:选择登录时触发NameError
问题:登录功能触发
NameError: name 'profiles' is not defined 选择登录选项(选项2)时触发NameError,提示“name 'profiles' is not defined on line 81”。
涉及代码
注册文件(假设为task2B.py)
import datetime from task2 import * # 存储用户数据的列表 profiles = [] def signUp(): Customer_name = input("Please enter your name : ") phone_number = input("Please enter your mobile number : ") pwd = input("Please enter your Password : ") confirm_Pwd = input("Please confirm your Password : ") dob = input("please Enter your Date of Birth # DD/MM/YY (No Space) : ") # 手机号格式验证 if phone_number[0] != 0 and len(phone_number) != 10: print("You have enter an invalid Phone number format") print("Please try again:") return False # 密码末尾数字验证 if pwd[-1].isnumeric() == False: print("Password should end with number") print("Please try again:") return False # 密码特殊字符验证 character = ['@', '#', '$'] result = [ele for ele in character if (ele in pwd)] if result == False: print("Password should include @,# or $") print("Please try again:") return False # 密码一致性验证 if pwd != confirm_Pwd: print("Your password are not matching") print("Please start again:") return False # 日期格式验证 out = True try: day, month, year = dob.split('/') datetime.datetime(int(year), int(month), int(day)) except ValueError: out = False if (out == False): print("Date should be in correct format") print("Please try again:") return False # 年龄验证 dob_year = dob[-3:] if (2022 - int(dob_year)) < 21: print("Your age should be at least 21 year old") print("Please try again:") return False # 保存用户数据 lst = [Customer_name, dob, phone_number, pwd, confirm_Pwd] profiles.append(lst) print("You have successfully signed up") # 主循环 while True: print("Please enter 1 for sign up.") print("Please enter 2 for log in.") print("Please enter 3 for Quite.") choice = input() if choice == "1": signUp() if choice == "2": loginprofiles() if choice == "3": print("Thank you for using the Application") break
登录文件(假设为task2.py)
from task2B import * def loginprofiles(): login_attempt = 0 while (True): user_name = input("Please enter your username (mobile number) ") pwd = input("Please enter your password ") login_flag = False # 遍历用户数据列表验证登录 for list in profiles: for i in range(0, len(list)): if list[1] == user_name and list[2] == pwd: login_flag = True name = list[0] # 登录失败处理 if login_flag == False: print("You have entered a wrong password") print("please try again") login_attempt = login_attempt + 1 if login_attempt == 3: reset_pass(profiles) break else: print("You have successfully signed in ") print("welcome " + name) break # 登录后操作选项 while (True): print("Please enter 1 for resetting the password :") print("Please enter 2 for sign out") choice = input() if (choice == "1"): reset(profiles) if (choice == "2"): print("you have sucessfuly logged out") break
错误原因
- 循环导入与变量作用域冲突:注册文件导入登录模块,登录模块又反向导入注册模块,形成循环导入,导致
profiles变量无法被登录模块正确识别。 - 变量传递缺失:登录函数
loginprofiles()尝试直接访问注册模块的全局变量profiles,但跨模块的全局变量无法直接共享,必须通过显式传递或独立存储模块实现。
修复方案
方案1:显式传递profiles变量
修改注册文件中调用登录函数的代码,将profiles作为参数传入:
# 注册文件中选项2的代码 if choice == "2": loginprofiles(profiles)
同时修改登录文件的loginprofiles()函数,接收参数:
def loginprofiles(profiles): # 添加参数接收用户数据列表 login_attempt = 0 while (True): user_name = input("Please enter your username (mobile number) ") pwd = input("Please enter your password ") login_flag = False # 后续代码保持不变,直接使用传入的profiles参数
方案2:独立存储用户数据模块
创建一个单独的data_store.py文件存储全局数据:
# data_store.py profiles = []
修改注册文件的导入语句:
import datetime from task2 import * from data_store import profiles # 从独立模块导入数据列表
修改登录文件的导入语句:
from data_store import profiles # 从独立模块导入数据列表
额外代码逻辑修正
除了NameError,代码中还有几处逻辑错误需要修复:
- 手机号验证逻辑:
phone_number[0] != 0应改为phone_number[0] != "0",且逻辑判断应为or(任一条件不满足即报错) - 密码特殊字符验证:
if result == False:应改为if not result:(空列表在布尔判断中为False) - 年龄计算逻辑:
dob_year = dob[-3:]会截取错误的年份字符串,应使用分割后的year变量 - 登录索引匹配:
list[1] == user_name and list[2] == pwd应改为list[2] == user_name and list[3] == pwd(对应注册时的手机号和密码索引)
内容的提问来源于stack exchange,提问作者Dexter
相关产品推荐
相关产品推荐

