python20讲课后作业动手2
str1 = ''' ABSaDKSbRIHcRHGcdDIF '''countA = 0
countB = 0
countC = 0
length = len(str1)
for i in range(length):
if str1 == '\n':
continue
if str1.isupper(): #问题1:continue循环三次,找到了三个大写字母ABS,因此countA == 3对吗?
if countB == 1:
countC += 1
countA = 0
else:
countA += 1
continue
if str1.islower() and countA == 3:
countB = 1
countA = 0
target = i
continue #问题2:这个continue有何意义,小写字母a已经被找出来了,且a的前面有三个大写字母
if str1.islower() and countC == 3: #问题3:如果a是被找出来的小写字母,按第一轮看countC根本没有加啊,这样不就判断不了小写a了,又得重新for循环了,我有点懵?
print(str1,end = '')
countA = 0
countB = 0
countC = 0 有题目吗?你这个程序要实现什么功能? 回答1.continue循环三次后,countA == 3,正确
回答2.这个continue很关键的,如果没有的话,会继续运行下去,将countA,countB,countC的值都变成了0,这样的话,之前的努力都白费了
回答3.countc的值在代码的第十二行处进行了累加. 一个账号 发表于 2020-2-3 17:25
有题目吗?你这个程序要实现什么功能?
打印出小写字母,小写字母左边有三个大写字母,右边也有三个大写字母 小喵学计算机 发表于 2020-2-3 17:30
回答1.continue循环三次后,countA == 3,正确
回答2.这个continue很关键的,如果没有的话,会继续运行下去,将 ...
关于回答3你说在第12行处累加了,我回去看了一下,满足条件countB ==1才会执行count+1呀。可是刚开始运行程序的时候countB一直是 0 呀,在第19行才会变成1。 小喵学计算机 发表于 2020-2-3 17:30
回答1.continue循环三次后,countA == 3,正确
回答2.这个continue很关键的,如果没有的话,会继续运行下去,将 ...
求指教 这是哪里的答案?和小甲鱼的不一样啊? 1)if str1.isupper(): #问题1:continue循环三次,找到了三个大写字母ABS,因此countA == 3对吗?对
if countB == 1:
countC += 1
countA = 0
else:
countA += 1
这个先循环三次,然后进入
2)if str1.islower() and countA == 3:
countB = 1
countA = 0
target = i
continue
continue意义:没有的话不会循环这个,会直接循环下一个语句,这时候获得 countB = 1, 这时候countC=0, 不满足下一个条件if str1.islower() and countC == 3: ,返回上面开始1)
然后 1)代码又执行3次,到了DKS,因为countB == 1所以满足条件, countC 也每次+1 到了countC == 3了, countA=0,这时候就到了
3)if str1.islower() and countC == 3:
print(str1,end = '') 这里,就打出来了,然后再执行后面初始化的语句,重新开始找
页:
[1]