鱼C论坛

 找回密码
 立即注册
查看: 663|回复: 8

[已解决]校验密码是否合法:编写程序提示用户输入一个密码,要求长度在5-10位,密码里必须...

[复制链接]
发表于 2023-12-4 19:58:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
校验密码是否合法:编写程序提示用户输入一个密码,要求长度在5-10位,密码里必须包含大写字母、小写字母和数字。根据用户输入会返回相应提示信息:如果密码长度不合法,返回“The length of password must in range of 5-10”;如果密码内容不合法,返回"Password must include uppercase,lowercase and digit",否则返回“Password input success”。提示:可利用S.isdisjoint(T)方法,如果集合S与T没有相同元素,返回True。

是哪儿出错了啊

password=input()
numlower=0
numisupper=0
numdigit=0
if 5<=len(password)<=10:
     for p in password:
          if p.islower():
              numlower+=1
         elif p.isupper():
             numisupper+=1
         elif p.isdigit():
             numdigit+=1
    if numlower>0 and numdigit> and numisupper>0:
        print('Password input success')
    else:
          print('Password must include uppercase,lowercase and digit')
else:
      print('The length of password must in range of  5-10')
最佳答案
2023-12-4 20:50:20
        代码的逻辑和语法基本上没有问题,重点检查代码的缩进,每级4个空格,必须严格遵守。
  1. password = input()
  2. numlower = 0
  3. numisupper = 0
  4. numdigit = 0
  5. if 5 <= len(password) <= 10 :
  6.     for p in password:
  7.         if p . islower():
  8.             numlower += 1
  9.         elif p . isupper():
  10.             numisupper += 1
  11.         elif p . isdigit():
  12.             numdigit += 1
  13.     if numlower > 0 and numdigit > 0 and numisupper > 0 :
  14.         print('Password input success')
  15.     else:
  16.         print('Password must include uppercase,lowercase and digit')
  17. else:
  18.     print('The length of password must in range of  5-10')
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-4 19:58:44 | 显示全部楼层
代码中有几个问题导致错误:

1. 在判断密码长度是否合法时,使用了 `if 5 and numisupper>0:` 的语句,这是错误的。应该使用条件判断 `if 5 <= len(password) <= 10:` 来判断密码长度是否在合法范围内。

2. 变量 `numisupper` 并未正确计算大写字母的数量,也没有计算小写字母和数字的数量。你可以使用循环遍历密码中的每个字符,并使用 `isupper()`、`islower()` 和 `isdigit()` 方法来判断字符的类型,并相应地增加统计变量的值。

3. 在密码内容不合法的情况下,应该返回提示信息而不是打印,可以使用 `return` 语句替代 `print` 语句。

下面是修改后的代码:
  1. password = input()
  2. num_lower = 0
  3. num_upper = 0
  4. num_digit = 0
  5. for char in password:
  6.     if char.isupper():
  7.         num_upper += 1
  8.     elif char.islower():
  9.         num_lower += 1
  10.     elif char.isdigit():
  11.         num_digit += 1
  12. if 5 <= len(password) <= 10:
  13.     if num_lower > 0 and num_upper > 0 and num_digit > 0:
  14.         print('Password input success')
  15.     else:
  16.         print('Password must include uppercase, lowercase, and digit')
  17. else:
  18.     print('The length of password must be in the range of 5-10')
复制代码

希望以上解答对您有所帮助!

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-4 19:59:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-4 19:59:58 | 显示全部楼层

非常抱歉,前面的回答出现了一些错误。以下是修正后的代码:
  1. password = input()
  2. num_lower = 0
  3. num_upper = 0
  4. num_digit = 0
  5. if len(password) >= 5 and len(password) <= 10:
  6.     for char in password:
  7.         if char.islower():
  8.             num_lower += 1
  9.         elif char.isupper():
  10.             num_upper += 1
  11.         elif char.isdigit():
  12.             num_digit += 1
  13.    
  14.     if num_lower > 0 and num_upper > 0 and num_digit > 0:
  15.         print('Password input success')
  16.     else:
  17.         print('Password must include uppercase, lowercase and digit')
  18. else:
  19.     print('The length of password must be within 5-10')
复制代码

这段代码首先获取用户输入的密码,并初始化三个变量用于记录大写字母、小写字母和数字的数量。然后,通过循环遍历密码中的每个字符,并使用`islower()`、`isupper()`和`isdigit()`方法来判断字符的类型,分别对应增加对应的计数器。最后,根据计数器的结果判断密码是否符合要求并输出相应的提示信息。

希望这次能帮到您!如果还有其他问题,请随时提问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-12-4 20:50:20 | 显示全部楼层    本楼为最佳答案   
        代码的逻辑和语法基本上没有问题,重点检查代码的缩进,每级4个空格,必须严格遵守。
  1. password = input()
  2. numlower = 0
  3. numisupper = 0
  4. numdigit = 0
  5. if 5 <= len(password) <= 10 :
  6.     for p in password:
  7.         if p . islower():
  8.             numlower += 1
  9.         elif p . isupper():
  10.             numisupper += 1
  11.         elif p . isdigit():
  12.             numdigit += 1
  13.     if numlower > 0 and numdigit > 0 and numisupper > 0 :
  14.         print('Password input success')
  15.     else:
  16.         print('Password must include uppercase,lowercase and digit')
  17. else:
  18.     print('The length of password must in range of  5-10')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-12-7 18:37:31 | 显示全部楼层
jackz007 发表于 2023-12-4 20:50
代码的逻辑和语法基本上没有问题,重点检查代码的缩进,每级4个空格,必须严格遵守。

谢谢你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-7 18:38:50 | 显示全部楼层
FishC_GPT 发表于 2023-12-4 19:59
非常抱歉,前面的回答出现了一些错误。以下是修正后的代码:

谢谢你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-7 18:39:03 | 显示全部楼层

不客气,我很高兴能帮到你!如果还有其他问题或者需要进一步的帮助,请随时提问。祝你编程愉快!{:5_137:}

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-20 20:48:22 | 显示全部楼层
{:5_137:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 09:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表