鱼C论坛

 找回密码
 立即注册
查看: 1460|回复: 6

[已解决]第二十课的课后习题,使用论坛的代码,自己弄了一个字符串,输出和预期不一致

[复制链接]
发表于 2017-12-20 15:31:58 | 显示全部楼层 |阅读模式

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

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

x
第二十课的课后习题(在字符串中找密码,密码为小写,每位密码左右均有且只有3位大写字母),使用论坛的代码,自己弄了一个字符串,输出(zoa)和预期(zhaocaiy)不一致,帮忙看下是什么问题?
  1. str1 = '''SSSzEWQhDDGaRGHoPGPcNDKaSDDiSOFyPLK'''
  2. countA = 0 #前面三个大写的计数
  3. countB = 0 #前面有三个大写,后面一个是小写,就置countB = 1,实际上应该用一个布尔量更清楚
  4. countC = 0 #前面两个条件都满足,又找到大写的计数,如果 countC == 3 说明找到一个密码字母
  5. length = len(str1)
  6. for i in range(length):
  7.     if str1[i] == '\n': #防止换行对判断的干扰
  8.         continue
  9.     if str1[i].isupper(): #如果找到的是大写
  10.         if countB == 1: #并且前面两个条件已经满足
  11.             countC += 1 #计数第三个条件
  12.             countA = 0
  13.         else:
  14.             countA += 1 #前面两个条件不满足后又找到大写,计数第一个条件
  15.         continue
  16.     if str1[i].islower() and countA == 3: #找到小写且第一个条件满足,即满足前两个条件
  17.         countB = 1
  18.         countA = 0
  19.         target = i #可能是密码字母,暂存下
  20.         continue
  21.     if str1[i].islower() and countC == 3: #如果找到小写,并且前面三个条件都满足,说明暂存的就是密码字母
  22.         print(str1[target], end='')
  23.     countA = 0
  24.     countB = 0
  25.     countC = 0
复制代码
最佳答案
2017-12-20 16:28:39
nevermoree 发表于 2017-12-20 15:59
是不是论坛的代码有问题?应该怎么调整代码呢?
  1. str1 = '''SSSzEWQhDDGaRGHoPGPcNDKaSDDiSOFyPLK'''
  2. length = len(str1)
  3. for i in range(length-7):
  4.    str2 = str1[i:i+7]
  5.    count = 0
  6.    for j in range(7):
  7.       if  str2[j].isupper():
  8.          count += 1
  9.       elif str2[j].islower():
  10.          count -= 1
  11.    if count == 5 and str2[3].islower():
  12.          print(str2[3],end = "")
复制代码


调整还不如自己写。上面是我写的,希望能给你提供思路。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-20 15:54:03 | 显示全部楼层
我输出的是'zaci';
出现的问题:1.一直循环到 h 才输出第一个小写 z; 2. z 的后三位也就是 h 的前三个是大写,所以countB = 3,  但是countA !=3,所以它会跳小写字母输出,后面的第偶数个小写没被输出也是这个原因。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-20 15:59:20 | 显示全部楼层
°蓝鲤歌蓝 发表于 2017-12-20 15:54
我输出的是'zaci';
出现的问题:1.一直循环到 h 才输出第一个小写 z; 2. z 的后三位也就是 h 的前三个是 ...

是不是论坛的代码有问题?应该怎么调整代码呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 16:28:39 | 显示全部楼层    本楼为最佳答案   
nevermoree 发表于 2017-12-20 15:59
是不是论坛的代码有问题?应该怎么调整代码呢?
  1. str1 = '''SSSzEWQhDDGaRGHoPGPcNDKaSDDiSOFyPLK'''
  2. length = len(str1)
  3. for i in range(length-7):
  4.    str2 = str1[i:i+7]
  5.    count = 0
  6.    for j in range(7):
  7.       if  str2[j].isupper():
  8.          count += 1
  9.       elif str2[j].islower():
  10.          count -= 1
  11.    if count == 5 and str2[3].islower():
  12.          print(str2[3],end = "")
复制代码


调整还不如自己写。上面是我写的,希望能给你提供思路。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 16:37:08 | 显示全部楼层
  1. str1 = '''SSSzEWQhDDGaRGHoPGPcNDKaSDDiSOFyPLK'''
  2. def getPW(string):
  3.         pw = []
  4.         length = len(string)
  5.         #  用while循环,如果当前index符合要求,可以直接index+=3, else index+=1
  6.         for index in range(3, length-3):
  7.                 if string[index].islower() and string[index-3:index].isupper() and string[index-3:index].isalpha() and string[index+1:index+4].isupper() and string[index+1:index+4].isalpha():
  8.                         pw.append(string[index])
  9.         return ''.join(pw)

  10. print(getPW(str1))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-20 16:43:12 | 显示全部楼层
  1. #  改为while循环
  2. str1 = '''SSSzEWQhDDGaRGHoPGPcNDKaSDDiSOFyPLK'''
  3. def getPW(string):
  4.         pw = []
  5.         length = len(string)
  6.         index = 3
  7.         while index < length-3:
  8.                 if string[index].islower() and string[index-3:index].isupper() and string[index-3:index].isalpha() and string[index+1:index+4].isupper() and string[index+1:index+4].isalpha():
  9.                         pw.append(string[index])
  10.                         index += 3
  11.                 else:
  12.                         index += 1
  13.         return ''.join(pw)

  14. print(getPW(str1))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-16 11:03:13 | 显示全部楼层


你的这种方式,如果小写字母前后有4个大写字母也会将小写字母输出啊。并不满足题目要求(有且仅有3个大写字母)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-4 23:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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