简体   繁体   English

错误方法未定义,即使它看起来正确

[英]Error method is not defined even though it seems correct

I would like to check an argument which user types on python.我想检查用户在 python 上键入的参数。 I wrote a method to check if the argument is half-width character.我写了一个方法来检查参数是否是半角字符。 You may not familiar if you are an English speaker, but some languages use the 2-word widths for 1 word.如果您是说英语的人,您可能不熟悉,但有些语言使用 2 个单词的宽度表示 1 个单词。 So I just wanted to write a method to check if a user is not using a unique language.所以我只想写一个方法来检查用户是否没有使用独特的语言。 The method name is "has_digit(password)"方法名称是“has_digit(password)”

here is my entire code这是我的整个代码

from sys import argv 
import re

if 1 >= len(argv):
    print("Please type an argument")
    exit()
else:
    print("Checking password strength")

password = argv[1]

if has_digit(password) == True and has_lower_letter(password) == True and has_upper_letter(password) == True:
    print("This password is strong")
else:
    print("This passwor is weak. Please choose another password")

def has_digit(password):
    """
    パスワードに半角数字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        半角数字を含んでいればTrue
    """
    m = re.search(r'[0-9]', password)
    return True if m else False


def has_lower_letter(password):
    """
    パスワードに英字小文字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        英字小文字を含んでいればTrue
    """
    m = re.search(r'[a-z]', password)
    return True if m else False


def has_upper_letter(password):
    """
    パスワードに英字大文字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        英字大文字を含んでいればTrue
    """
    m = re.search(r'[A-Z]', password)
    return True if m else False

error message错误信息

  File "pw_check.py", line 12, in <module>
    if has_digit(password) == True and has_lower_letter(password) == True and has_upper_letter(password) == True:
NameError: name 'has_digit' is not defined

How can I fix this error?我该如何解决这个错误? Thank you in advance.先感谢您。

It's just how python parses your file.这就是 python 解析文件的方式。 Your function should be defined before you call it, like:你的 function 应该在你调用它之前定义,比如:

from sys import argv 
import re
def has_digit(password):
    """
    パスワードに半角数字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        半角数字を含んでいればTrue
    """
    m = re.search(r'[0-9]', password)
    return True if m else False


def has_lower_letter(password):
    """
    パスワードに英字小文字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        英字小文字を含んでいればTrue
    """
    m = re.search(r'[a-z]', password)
    return True if m else False


def has_upper_letter(password):
    """
    パスワードに英字大文字が含まれるかをチェックします。
    Args:
        チェック対象のパスワード
    Returns:
        英字大文字を含んでいればTrue
    """
    m = re.search(r'[A-Z]', password)
    return True if m else False
if 1 >= len(argv):
    print("Please type an argument")
    exit()
else:
    print("Checking password strength")

password = argv[1]

if has_digit(password) == True and has_lower_letter(password) == True and has_upper_letter(password) == True:
    print("This password is strong")
else:
    print("This passwor is weak. Please choose another password")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使一切似乎都很好,我也会得到一个定义的错误 - I am getting a defined error even though everything seems to be fine 即使看起来正确也无法创建表面 - cannot create surface even though it seems correct 变量未定义错误,即使它是? - Variable not defined error, even though it is? 程序卡在 while 循环中,即使它看起来是正确的 - Program stuck in while loop even though it seems correct 为什么在python中出现错误,提示即使我先前定义了方法也找不到方法? - Why do I get an error in python saying that a method cannot be found even though I previously defined the method? 即使定义了属性,NoneType 也会出错? - NoneType Error even though attribute is defined? 属性未定义错误,即使它是在全局 scope 上定义的 - Attribute not defined error, even though it is defined on a global scope 即使实例由方法返回,Python selenium 驱动程序实例也未定义 - Python selenium driver instance not defined even though instance is returned by method 即使定义了属性,实例方法也会引发AttributeError - Instance method raises AttributeError even though the attribute is defined Python tkinter 错误:“变量未定义”,即使它已定义 - Python tkinter error:'Variable is not defined" even though it is define
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM