Python ATM登录验证逻辑异常:账户不存在时提示功能失效问题排查
Let's break down why your code fails when an invalid account number is entered, and fix it step by step.
The Root Cause
Your line correct_pin= valid_pins[valid_accounts.index(account)] runs before you check if the account exists in valid_accounts. If the account isn't in the list, valid_accounts.index(account) throws a ValueError (since it can't find the account to get its index), and your code crashes before reaching the invalid login check.
Quick Fix (Keep Existing List Structure)
We need to rearrange the logic to first verify the account exists, then fetch the corresponding PIN only if it does:
account_name = ['Adetunji Michael', 'Dina Asher-Smith', 'Niyola Davidson', 'John Paul'] valid_accounts= [3455590445, 2871290429, 3599076267, 1234567890] valid_pins = [34777, 45786, 24055, 12345] account_balance= [112000, 45700, 2300, 30000] # Interface setup print(" *************************") print(" ABC BANK OF NIGERIA.\n MARVELLING YOU SINCE 2003\n WELCOME TO THE ATM MACHINE.") print(" *************************") trials=3 while trials!=0: account= int(input(" PLEASE INPUT YOUR ACCOUNT NUMBER: ")) pin = int(input(" PLEASE INPUT YOUR 5 DIGIT PIN: ")) # First check if account exists if account not in valid_accounts: print(" INVALID LOGIN DETAILS. ") trials -=1 print(" YOU HAVE ", trials, " TRIALS LEFT") else: # Now safely get the matching PIN since account is valid account_index = valid_accounts.index(account) correct_pin = valid_pins[account_index] if pin != correct_pin: print(" INVALID LOGIN DETAILS. ") trials -=1 print(" YOU HAVE ", trials, " TRIALS LEFT") else: print(f"Welcome {account_name[account_index]}!") # Add your post-login logic here (balance check, withdrawal, etc.) break # Exit loop after successful login
Better Approach: Use Dictionaries for Data Association
Storing related data (account, PIN, name, balance) in separate lists is error-prone (easy to mix up indices). A cleaner way is to use a dictionary where each account number maps to its details:
# Organize data into a single dictionary for clarity and safety customer_data = { 3455590445: {"name": "Adetunji Michael", "pin": 34777, "balance": 112000}, 2871290429: {"name": "Dina Asher-Smith", "pin": 45786, "balance": 45700}, 3599076267: {"name": "Niyola Davidson", "pin": 24055, "balance": 2300}, 1234567890: {"name": "John Paul", "pin": 12345, "balance": 30000} } # Interface setup print(" *************************") print(" ABC BANK OF NIGERIA.\n MARVELLING YOU SINCE 2003\n WELCOME TO THE ATM MACHINE.") print(" *************************") trials=3 while trials!=0: account= int(input(" PLEASE INPUT YOUR ACCOUNT NUMBER: ")) pin = int(input(" PLEASE INPUT YOUR 5 DIGIT PIN: ")) # Check if account exists in our data if account not in customer_data: print(" INVALID LOGIN DETAILS. ") trials -=1 print(" YOU HAVE ", trials, " TRIALS LEFT") else: # Verify PIN against the stored value if pin != customer_data[account]["pin"]: print(" INVALID LOGIN DETAILS. ") trials -=1 print(" YOU HAVE ", trials, " TRIALS LEFT") else: print(f"Welcome {customer_data[account]['name']}!") # Access balance or other data easily: customer_data[account]['balance'] break
This approach eliminates index-related bugs entirely and makes your code much easier to maintain when adding/removing customers.
内容的提问来源于stack exchange,提问作者user16342430

