[英]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.