You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python密码检查器函数调用与循环逻辑修正技术求助

Fixing Your Password Checker Implementation in Python

Let's fix your password checker step by step. I'll walk through the issues in your code and provide a fully working version with explanations:

Fully Corrected Code

def password_checker(password):
    is_valid = True
    num_count = 0
    # Check if password contains only alphanumeric characters
    if not password.isalnum():
        print("Error! Only letters and numbers are allowed.")
        is_valid = False
    # Check for spaces (redundant with isalnum but gives clearer feedback)
    if ' ' in password:
        print("Error! No spaces allowed.")
        is_valid = False
    # Check length requirement (greater than 7 = at least 8 characters)
    if len(password) <= 7:
        print("Error! Password must be longer than 7 characters.")
        is_valid = False
    # Count number of digits in the password
    for char in password:
        if char.isdigit():
            num_count += 1
    # Ensure at least 2 digits are present
    if num_count < 2:
        print("Error! Password must contain at least 2 numbers.")
        is_valid = False
    # Ensure at least one letter is present
    if not any(char.isalpha() for char in password):
        print("Error! Password needs letters.")
        is_valid = False
    return is_valid

# Loop for up to 5 password attempts
for attempt in range(5):
    password = input("Enter password: ")
    if password_checker(password):
        print("Valid password!")
        break
    else:
        remaining = 4 - attempt
        print(f"Invalid password. {remaining} attempt{'s' if remaining !=1 else ''} remaining.\n")
else:
    # Runs only if the loop completes all 5 iterations without breaking
    print("You've used all 5 attempts. Access denied.")

Key Issues Fixed & Explanations

Let's break down what was wrong in your original code and how we fixed it:

  • Function didn't return a value: Your password_checker set a boolean a but never returned it. Without a return value, the function returns None by default, making your loop's if condition useless. We added return is_valid to pass the validation result back to the loop.
  • Incorrect string methods & logic:
    • password.isnum() is not a valid method—we used isalnum() to ensure the password only contains letters and numbers (covering the "only allow digits and letters" requirement).
    • Your length check was backwards: you checked len(password)<=8 but the requirement is "length greater than 7" (so len(password) <=7 is the error condition).
    • The digit counting logic was broken: you tried to count integers in a string, which won't work. We iterate over each character in the password and count digits directly.
  • Loop logic mistakes:
    • You only asked for the password once outside the loop—we moved password = input(...) inside the loop so the user gets prompted each attempt.
    • You were passing the loop index i to password_checker instead of the actual password string.
    • The if condition in the loop was backwards: we check if password_checker(password) returns True (valid) to print success and break, else show an error with remaining attempts.
  • Missing validation checks: We added an explicit check to ensure the password contains at least one letter (since the requirement says it needs both letters and numbers), which your original code didn't handle properly.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:44:43