【问题标题】:Can someone help to fix code for function arguments in python有人可以帮助修复python中函数参数的代码吗
【发布时间】:2021-02-19 13:14:02
【问题描述】:

所以我正在尝试制作密码强度检查器,但在尝试检查密码的正确输入长度然后尝试测试运行代码时它显示未定义的名称“pwd”第 12 行。但是我无法找到解决方法这。请帮忙!谢谢。

def pwd_validate(pwd):
# Validate the length of the password
    while True:
        pwd = input("Please enter your new password, it should be 12 characters long: ")
        if len(pwd) == 12:
            print("""Thank you for entering a 12 charcter password 
            Now checking strength of password...""")
            break
        else:
            print("Password must contain only 12 characters please:")

pwd_validate(pwd)

【问题讨论】:

  • 不使用pwd为什么要作为参数?只需将定义更改为def pwd_validate():,然后调用为pwd_validate()
  • 你传递给pwd_validate的是什么?不能是该函数本身从用户那里获得的密码……

标签: python python-3.x function arguments


【解决方案1】:

你传递给函数的密码没有定义,它至少应该是一个空字符串。只需在将其传递给函数之前定义它

def pwd_validate(pwd):
# Validate the length of the password
    while True:
        pwd = input("Please enter your new password, it should be 12 characters long: ")
        if len(pwd) == 12:
            print("""Thank you for entering a 12 charcter password 
            Now checking strength of password...""")
            break
        else:
            print("Password must contain only 12 characters please:")

pwd = ""
pwd_validate(pwd)

【讨论】:

  • 虽然确实没有理由这样做,因为pwd 在使用之前已在函数内部设置。
  • 是的,这是做同样事情的不同方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2014-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
相关资源
最近更新 更多