Python break语句未生效问题:触发冻结条件后循环仍运行
解决账号冻结无法终止程序的问题
当前代码中,触发冻结条件时的break仅能退出内层循环,外层while True会继续执行。以下是两种简单有效的解决方式:
方法一:直接终止程序(推荐)
使用sys.exit()函数可以直接终止整个程序,无需处理外层循环。步骤如下:
- 在代码开头导入
sys模块:
import time import sys # 新增导入
- 将所有触发冻结逻辑中的
break替换为sys.exit():- 手机号错误时:
if phonenumberQ != phonenum: time.sleep(1) print('Phone number incorrect. Account frozen.') sys.exit() # 替换break- 用户名尝试次数过多时:
if userTries > 2: time.sleep(1) print('Too many attempts, account frozen.') sys.exit() # 替换break- 密码部分的两处冻结逻辑同理,替换
break为sys.exit()
方法二:使用标志变量控制外层循环
通过设置一个布尔标志,在触发冻结时标记状态,外层循环检测到该状态后终止:
- 在外层循环前定义标志变量:
userTries = 0 passTries = 0 account_frozen = False # 新增标志
触发冻结时设置标志并退出内层循环:
- 用户名模块的手机号错误:
if phonenumberQ != phonenum: time.sleep(1) print('Phone number incorrect. Account frozen.') account_frozen = True break- 用户名尝试次数过多:
if userTries > 2: time.sleep(1) print('Too many attempts, account frozen.') account_frozen = True break- 密码模块的两处冻结逻辑同样设置
account_frozen = True后break
内层循环结束后,在外层循环中检查标志,若冻结则终止:
在用户名内层循环结束后添加:if account_frozen: break在密码内层循环结束后也添加同样的检查:
if account_frozen: break
修改后的完整代码示例(以方法一为例)
import time import sys userTries = 0 passTries = 0 while True: # welcome user print('Welcome to [WEBSITE].') print('You must create an account to continue.') # variable inputs time.sleep(1) username = input('Please enter a username: ') password = input('Please enter a password: ') phonenum = int(input('Please enter your phone number for user confirmation: ')) # account creation confirmation time.sleep(1) print('Welcome,', username + '.') print('Thank you for creating your account!') # re-enter username + password time.sleep(1) usernameQ = input('Please re-enter your username: ') while usernameQ != username: # username checker userTries += 1 time.sleep(1) newusernameQ = input('Username incorrect, would you like to make a new username? (y or n) ') # (not) creating new username while newusernameQ not in ('Y', 'y', 'N', 'n'): time.sleep(1) print('Invalid answer.') newusernameQ = input('Would you like to make a new username? (y or n)') # phone number confirmation if newusernameQ in ('Y', 'y'): phonenumberQ = int(input('What is your phone number? ')) if phonenumberQ != phonenum: time.sleep(1) print('Phone number incorrect. Account frozen.') sys.exit() else: # re doing username time.sleep(1) username = input('Please enter a new username: ') time.sleep(1) print('Username set.') time.sleep(1) usernameQ = input('Please re-enter your username: ') else: # username ask usernameQ = input('Please re-enter your username: ') # tries too many if userTries > 2: time.sleep(1) print('Too many attempts, account frozen.') sys.exit() # asking for first password time.sleep(1) passwordQ = input('Please re-enter your password: ') # password checker while passwordQ != password: passTries += 1 time.sleep(1) newpasswordQ = input('Password incorrect, would you like to make a new password? (y or n) ') # (not) creating new password while newpasswordQ not in ('Y', 'y', 'N', 'n'): time.sleep(1) print('Invalid answer.') newpasswordQ = input('Would you like to make a new password? (y or n)') # phone number confirmation if newpasswordQ in ('Y', 'y'): phonenumberQ = int(input('What is your phone number? ')) if phonenumberQ != phonenum: time.sleep(1) print('Phone number incorrect. Account frozen.') sys.exit() else: # re doing password time.sleep(1) password = input('Please enter a new password: ') time.sleep(1) print('Password set.') time.sleep(1) passwordQ = input('Please re-enter your password: ') else: # password ask passwordQ = input('Please re-enter your password: ') # tries too many if passTries > 2: time.sleep(1) print('Too many attempts, account frozen.') sys.exit() if passwordQ == password and usernameQ == username: for i in range(2): time.sleep(1) print('Logging in...') time.sleep(1) print('Login Successful.') time.sleep(1) print('Welcome, ' + username + '.') break
内容的提问来源于stack exchange,提问作者Aidan Barratt
相关产品推荐
相关产品推荐

