密码检查 - Python 3 [英] Password check- Python 3

查看:125
本文介绍了密码检查 - Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个函数来检查一个字符串是否是有效的密码。



规则:
必须至少有8个字符
密码必须包含只有字母和数字
密码必须至少包含2位数字

继承人至今为止,我做错了什么?谢谢

  def getPassword():
password = input(Enter password:)
return密码

def validPassword(密码):
如果len(密码)> = 8:
valid = True
如果password.alnum():
valid = True
如果password.isdigit< 2:
valid = True
else:
返回False


def main():
password = validPassword()
如果validPassword(密码):
print(密码,有效)
else:
print(密码,无效)
main()


解决方案

这似乎是一项家庭作业,所以我会尽力避免回答,但更多的尝试推动你在正确的方向。



您的第一批代码将运行将是

  ... 
def main():
password = validPassword()#我们试图检查的密码
...

呃哦, validPassword(密码)并没有得到密码,也许你的意思是 getPassword()



让我们通过 validPassword(password)逐行

  ... 
def validPassword(密码):
如果len(密码)> = 8:
valid = True
...

可以检查字符串的长度是否超过8个字符,如果是,我们初始化变量valid并将其设置为True

<$ p $如果password.alnum():
valid = True
...
...
p $ p>

然后,不管发生了什么,我们称之为alnum(我认为它不是python中的函数,可能意味着isalnum。 )来检查密码中的所有字符是否都是数字。
如果是,我们初始化变量valid并将其设置为True。
您可能会说,但我已经初始化了它,不是真的,在python中有

  ... 
如果密码.isdigit< nofollow>范围
。 2:
valid = True
...

然后我们检查密码方法 isdigt 小于2,也许你的意思是 password.isdigit()我真的很谨慎,因为它是不清楚你在编程或python方面的熟练程度。但是如果你的意思是 password.isdigit()< 2 那么你问的密码是否是一个数字,如果是,是否小于2.
如果是,我们初始化变量有效并将其设置为True。

  ... 
其他:
返回False
...

然后当且仅当 password.isdigit()< 2 为假,我们返回false。



以下是一些指示:


  • 了解 python中的控制流程

  • 当你在这里问一个问题,而不是说,这里有问题,这里
    是我目前的代码,请帮助,说,这是问题,这是我的
    当前的代码,这是我期望在x,y,z和a,b,c是
    发生的事情,请帮助如果我们不知道你在哪里挣扎,
    我们该如何帮助您最好

  • 尝试并运行您的代码并向我们显示堆栈跟踪(如果存在),这里肯定会出现错误,python碰巧有更好的错误,大多数语言在我看来)



希望我一行一行的解释能帮助您找到一些错误,并更好地了解如何继续,如果没有,请随时修改您的问题,以便我们更好地了解如何提供帮助。



愉快的编码。

Write a function that checks whether a string is valid password.

Rules: Must have at least 8 characters A password must consist of only letters and digits A password must contain at least 2 digits

Heres what I have so far, what am i doing wrong? thank you

def getPassword():
   password = input("Enter password: ")
   return password

def validPassword(password):
   if len(password) >= 8:
      valid = True
   if password.alnum():
      valid = True
   if password.isdigit < 2:
      valid = True
   else:
      return False


def main():
   password = validPassword()
   if validPassword(password):
      print(password, "is valid")
   else:
      print(password, "is invalid")
main()

解决方案

This seems like a homework assignment so I'll try and stay from directly answering, but more try to push you in the right direction.

Your first bit of code that will run will be

...
def main():
    password = validPassword() # the password we are trying to check
...

Uh oh, validPassword(password) takes an argument and doesn't get a password, maybe you meant getPassword()

Lets go through the logic of validPassword(password) Line by line

...
def validPassword(password):
   if len(password) >= 8: 
      valid = True 
...

lets check if the length of the string is more than 8 characters, if it is, we initialize the variable valid and set it to True

...
   if password.alnum(): 
      valid = True 
...

Then regardless of what has happened, we call alnum, ( which I don't think is a function in python, probably meant isalnum. ) to check if all the characters in the password are numbers. If it is we initialize the variable valid and set it to True. You may say, but I already initialized it, well not really, in python there is scope.

...
   if password.isdigit < 2:
      valid = True
...

Then we check if the passwords method isdigt is less than 2, maybe you meant password.isdigit() I am really being meticulous as it is unclear your proficiency in programming or python. But if you meant password.isdigit() < 2 then you are asking if the password is a digit and if yes, is it less than 2. If it is we initialize the variable valid and set it to True.

...
   else:
      return False
...

Then if and only if password.isdigit() < 2 is false, we return false.

Here are some pointers:

  • Learn about control flow in python
  • When you ask a question here, rather than say, "here is problem, here is my current code, please help," say, "here is problem, here is my current code, this is what I expect on line x,y,z and a,b,c is happening, please help" if we do not know where you are struggling, how can we help you best
  • Try and run your code and show us the stacktrace ( if it exists ), there are definitely errors here that would have come up, python happens to have nicer errors that most languages (in my opinion)

Hopefully my line by line explanation has helped you find some of your errors and a better idea of how to continue, if not, feel free to amend your question so that we get a better idea of how to help.

Happy coding.

这篇关于密码检查 - Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆