鱼C论坛

 找回密码
 立即注册
查看: 2283|回复: 2

零基础学python第20讲第1题,在指定条件下查询密码

[复制链接]
发表于 2019-10-2 21:38:05 | 显示全部楼层 |阅读模式

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

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

x
原题:请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
    a) 每位密码为单个小写字母
    b) 每位密码的左右两边均有且只有三个大写字母

我写了一段代码但是跑不出来,不知道为什么
  1. str2 = str(input('请输入字符串:'))
  2. countA = 0  # 统计前边的大写字母
  3. countB = 0  # 统计小写字母
  4. countC = 0  # 统计后边的大写字母
  5. length = len(str2)
  6. for i in range(length):
  7.     while countA < 3:
  8.         if str2[i].isupper():
  9.             countA += 1
  10.             i += 1
  11.         
  12.     while countA == 3:
  13.         if str2[i].islower():
  14.             countB += 1
  15.             i += 1
  16.             break
  17.          
  18.     while countA == 3 and countB == 1:
  19.         while countC < 3:
  20.             if str2[i].isupper():
  21.                 countC += 1
  22.                 i += 1
  23.     if countA == 3 and countB == 1 and countC ==3:
  24.         print(str2[i-4])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-3 22:00:58 | 显示全部楼层
你的for循环有问题呀,很明显你还没有理解for循环是怎么执行的,判断大写字母后不进入下一个循环还i+1......你的目标是找到一串大写字母中的小写字母,遇到大写字母就要跳过。
str2 = str(input('请输入字符串'))    #注意这里input('请输入字符串')本身就是str,不需要再一次强制转换为str

result = ''
for i in range(length):
    if str[i].isupper():
        continue
    elif str[i].islower():
        item +=i
        continue
print(result)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-3 23:54:04 | 显示全部楼层
本帖最后由 XiaoPaiShen 于 2019-10-4 00:01 编辑
  1. # str2 = str(input('请输入字符串:'))
  2. str2 = 'ABCaBCDbGHEcFGDdGGGeABC'

  3. length = len(str2)

  4. index = 2
  5. password = []

  6. if length < 7:
  7.     print('此字符串中不可能含有密码!')
  8. else:   
  9.     for item in str2[3:-3]:
  10.         index += 1
  11.         if item.islower():
  12.             prev = str2[index-3:index]                #前面3个字符
  13.             suff = str2[index+1:index+4]                # 后面3个字符

  14.             # print(prev, suff)

  15.             if (not prev.isupper()) or (not suff.isupper()):       
  16.                 continue
  17.                     
  18.             if index > 3 and (length - index) > 4:        # 中间字符,并且前后都有多于3个字符
  19.                 if str2[index-4].isupper() or str2[index+4].isupper():
  20.                     continue
  21.                 else:                    
  22.                     password.append(item)

  23.             elif index == 3 and (length - index) > 4:        # 左边只有3个
  24.                 if str2[index+4].isupper():                    
  25.                     continue
  26.                 else:
  27.                     password.append(item)                                    

  28.             elif index > 3 and (length - index) == 4:        # 右边只有3个
  29.                 if str2[index-4].isupper():                    
  30.                     continue
  31.                 else:
  32.                     password.append(item)

  33.             else:                                                        # 前后都只有3个字符
  34.                 password.append(item)

  35. print(password)
  36.             
  37.             
  38.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-19 16:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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