20讲动动手01,从字符串里找密码
# 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:# a) 每位密码为单个小写字母
# b) 每位密码的左右两边均有且只有三个大写字母
代码目的如上,我写的这个跑不出来,不知道哪里有问题{:5_105:}
str2 = r'''ABSaDKSbRIHcRHGcdDIF'''
countA = 0
countB = 0
countC = 0
length = len(str2)
for i in range(length):
if str2 == '\n':
continue
if str2.isupper() == 1:
if countB:
countC += 1
else:
countA += 1
countC = 0
if str2.islower() == 1:
if countA == 3 and countB == 0:
countB = 1
target = i
else:
countA = 0
countB = 0
countC = 0
if countA == 3 and countC == 3:
if i+1 != length and str2.isupper()== 1:
countA = 0
countB = 0
countC = 0
else:
print(str2,end='')
countA = 3
countB = 0
countC = 0 本帖最后由 灰晨 于 2021-6-30 17:13 编辑
先回一下楼上,True或False等于1和0,所以这不是错误
错在
if countA == 3 and countC == 3:
if i+1 != length and str2.isupper()== 1:
把i+1 != lengt改为i+1 == lengt
就OK了
感觉
if i+1 != length and str2.isupper()== 1:
countA = 0
countB = 0
countC = 0
else:
这一段完全可以不要 字符串的isupper()和islower()方法,返回的是True或False 笨鸟学飞 发表于 2021-6-30 16:53
字符串的isupper()和islower()方法,返回的是True或False
{:10_245:}
本帖最后由 非凡 于 2021-7-1 21:02 编辑
if i+1 != length and str2.isupper()== 1:
第24行代码你改用下这个试试 {:9_230:}
页:
[1]