You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python新手求助:基础登录场景中列表append方法无法添加昵称和密码的问题

Fixing Your Python Login/Signup System Issues

Hey there! Let's work through fixing your login system—you've got the core idea down with lists, conditionals, and functions, just a few key tweaks will make it fully functional. Let's break down the problems and their fixes:

1. Incorrect Use of append()

The biggest issue is how you're using append(). This method modifies the list in-place and returns None—so when you do list1 = list1.append(id2), you're actually turning list1 into None instead of adding the new nickname.

Fix: Just call append() without reassigning the list:

list1.append(id2)
list2.append(pw2)

2. Unnecessary print() Inside input()

Your input() calls are wrapped in print(), which causes extra empty lines and unexpected behavior. The input() function can take a prompt string directly—you don't need to use print() here.

Bad:

op1 = input(print("Do you want to login or signup?"))

Good:

op1 = input("Do you want to login or signup? ")

3. Recursive main() Resets Your User Data

Every time you call main() again (after an error or signup), you're creating new empty list1 and list2—so all previous signups get erased. Instead of using recursion, use a loop to keep the program running, and define your user lists outside the loop so they persist.

4. Password Validation Doesn't Match the Correct User

Right now, you just check if the password exists in list2—but this doesn't link the password to the specific nickname. If two users have different nicknames but the same password, logging in with either nickname and that password would work incorrectly. Instead, find the index of the nickname in list1, then check if the password at that index in list2 matches.


Full Corrected Code

Here's the fixed version with all these changes, plus some extra clarity:

def main():
    # Define user lists outside the loop so they persist across sessions
    usernames = []
    passwords = []
    print("S M A R T L O G I N S Y S T E M")
    
    while True:
        op1 = input("Do you want to login or signup? ").lower()
        
        if op1 == "signup":
            id2 = input("What do you want your nickname to be? ")
            if id2 in usernames:
                print("Sorry, that username is taken. If it's yours, try the login option.")
                continue
            print(f"Great, your username is {id2}!")
            pw2 = input("What do you want to set your password as? ")
            # Append without reassigning
            usernames.append(id2)
            passwords.append(pw2)
            print(f"Great {id2}, your account is created! You can now login.")
        
        elif op1 == "login":
            id1 = input("Enter your nickname: ")
            if id1 in usernames:
                # Get the index of the username to match the password
                user_index = usernames.index(id1)
                pw1 = input("Enter your password: ")
                if pw1 == passwords[user_index]:
                    print(f"Welcome {id1}!\nYou are now logged in")
                    break  # Exit the loop once logged in
                else:
                    print("You have entered an incorrect password.")
            else:
                print("That username doesn't exist. Check again or signup.")
        
        else:
            print("Please enter either 'login' or 'signup'!")

if __name__ == "__main__":
    main()

Key Improvements in This Code:

  • Uses a while True loop instead of recursion to keep the program running without resetting data
  • Fixes the append() usage to preserve your user lists
  • Removes unnecessary print() calls inside input()
  • Validates passwords by matching the index of the username, ensuring the correct password is paired with the right account
  • Uses f-strings for cleaner string formatting (a nice Python feature to learn!)

内容的提问来源于stack exchange,提问作者SASANK

火山引擎 最新活动