yretsym 发表于 2017-1-11 15:02:14

python20讲,课后题求助!!

题:


我的解题思路:
1.先把小写字母的字符找到,把其字符的位置存在一个列表中
2.检查小写字母前后是不是大写
3。把符合条件的值打出来

a = '''粘贴字符'''
length = len(a)
temp = []
for i in range(length):
    if a == '\n':
      continue
    for each in a:
      if a.islower() == True and i + 4 <= length and i - 4 >=0 :
            temp.append(i)

length1 = len(temp)

for i1 in range(length1):
    if a+1].islower() == False   \
       and a+2].islower() == False \
       and a+3].islower() == False \
       and a-1].islower() == False \
       and a-2].islower() == False \
       and a-3].islower() == False \
       and a-4].islower() == True\
       and a+4].islower() == True:
      print(a],end="")

结果:


多了个u和d,搞不明白啊

alltolove 发表于 2017-1-11 15:29:35

本帖最后由 alltolove 于 2017-1-11 15:34 编辑

{:10_282:}

781318059 发表于 2017-1-11 15:44:22

{:9_218:}

seasonjj 发表于 2017-3-24 17:15:58

我最近也被这题困扰了很久,问题和你一样,也是多了俩个字母,最后幸亏得到了大神的点播
我看了下你的代码,和我出现的问题基本一样,都是因为你在判断小写字母前面和后面三个字符是否为大写的时候,忽略了换行符‘\n’在前后三个字符里的情况,你可以在a被定义后加上一行:
a = a.replace('\n','')

或者其实你把判断前后三个字符是否为大写写成这样:

    if a+1].isupper() == True   \
       and a+2].isupper() == True \
       and a+3].isupper() == True \
       and a-1].isupper() == True \
       and a-2].isupper() == True \
       and a-3].isupper() == True \
       and a-4].islower() == True\
       and a+4].islower() == True:


这样也就可以了

谢谢!单纯是想巩固自己学的内容,表达错误或者不好的地方忘谅解,另 我自己写的代码是这样的:
str2 = '粘贴字符'
#str2 = str2.replace('\n', '')
lenght = len(str2)
for i in range(3,lenght-4):
         
    x = str(str2)                  #定义字符x
    y = str(str2)                #定义字符x的前三个字符
    z = str(str2)            #定义字符x的后三个字符
    q = y+z
    a = str(str2)                  #定义字符x前面第四个字符
    b = str(str2)                  #定义字符x后面第四个字符
    if x.islower() == True and q.isupper() == True and '\n' not in str(q) and \
       a.isupper() == False and b.isupper() == False:
      print(x,end='')
页: [1]
查看完整版本: python20讲,课后题求助!!