python[课后作业] 第020讲 动动手1题找密码
在论坛看到了两个答案,有点懵,难道有一个是错的吗?求分析第一个代码。第一个答案:
str1 = ''' ABSaDKSbRIHcRHGcdDIF '''
countA = 0
countB = 0
countC = 0
length = len(str1)
for i in range(length):
if str1 == '\n':
continue
if str1.isupper():
if countB == 1:
countC += 1
countA = 0
else:
countA += 1
continue
if str1.islower() and countA == 3:
countB = 1
countA = 0
target = i
continue
if str1.islower() and countC == 3:
print(str1,end = '')
countA = 0
countB = 0
countC = 0
第二个代码:
str1 = '''ABSaDKSbRIHcRHGcdDIF'''
countA = 0# 统计前边的大写字母
countB = 0# 统计小写字母
countC = 0# 统计后边的大写字母
length = len(str1)
for i in range(length):
if str1 == '\n':
continue
"""
|如果str1是大写字母:
|-- 如果已经出现小写字母:
|-- -- 统计后边的大写字母
|-- 如果未出现小写字母:
|-- -- 清空后边大写字母的统计
|-- -- 统计前边的大写字母
"""
if str1.isupper():
if countB:
countC += 1
else:
countC = 0
countA += 1
"""
|如果str1是小写字母:
|-- 如果小写字母前边不是三个大写字母(不符合条件):
|-- -- 清空所有记录,重新统计
|-- 如果小写字母前边是三个大写字母(符合条件):
|-- -- 如果已经存在小写字母:
|-- -- -- 清空所有记录,重新统计(出现两个小写字母)
|-- -- 如果该小写字母是唯一的:
|-- -- -- countB记录出现小写字母,准备开始统计countC
"""
if str1.islower():
if countA != 3:
countA = 0
countB = 0
countC = 0
else:
if countB:
countA = 0
countB = 0
countC = 0
else:
countB = 1
countC = 0
target = i
"""
|如果前边和后边都是三个大写字母:
|-- 如果后边第四个字母也是大写字母(不符合条件):
|-- -- 清空记录B和C,重新统计
|-- 如果后边仅有三个大写字母(符合所有条件):
|-- -- 打印结果,并清空所有记录,进入下一轮统计
"""
if countA == 3 and countC == 3:
if i+1 != length and str1.isupper():
countB = 0
countC = 0
else:
print(str1, end='')
countA = 3
countB = 0
countC = 0 如果都运行正确,就是都没有问题的。 qiuyouzhi 发表于 2020-4-7 21:34
如果都运行正确,就是都没有问题的。
第一个运行不对,可以分析一下哪里出错了吗
页:
[1]