鱼C论坛

 找回密码
 立即注册
查看: 4920|回复: 3

[已解决]python20讲,课后题求助!!

[复制链接]
发表于 2017-1-11 15:02:14 | 显示全部楼层 |阅读模式

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

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

x
题:
sp170111_145156.png

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

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

  10. length1 = len(temp)

  11. for i1 in range(length1):
  12.     if a[temp[i1]+1].islower() == False     \
  13.        and a[temp[i1]+2].islower() == False \
  14.        and a[temp[i1]+3].islower() == False \
  15.        and a[temp[i1]-1].islower() == False \
  16.        and a[temp[i1]-2].islower() == False \
  17.        and a[temp[i1]-3].islower() == False \
  18.        and a[temp[i1]-4].islower() == True  \
  19.        and a[temp[i1]+4].islower() == True:
  20.         print(a[temp[i1]],end="")
复制代码


结果:
sp170111_145917.png

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


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

  1.     if a[temp[i1]+1].isupper() == True     \
  2.        and a[temp[i1]+2].isupper() == True \
  3.        and a[temp[i1]+3].isupper() == True \
  4.        and a[temp[i1]-1].isupper() == True \
  5.        and a[temp[i1]-2].isupper() == True \
  6.        and a[temp[i1]-3].isupper() == True \
  7.        and a[temp[i1]-4].islower() == True  \
  8.        and a[temp[i1]+4].islower() == True:
复制代码


这样也就可以了

谢谢!单纯是想巩固自己学的内容,表达错误或者不好的地方忘谅解,另 我自己写的代码是这样的:
  1. str2 = '粘贴字符'
  2. #str2 = str2.replace('\n', '')
  3. lenght = len(str2)
  4. for i in range(3,lenght-4):
  5.          
  6.     x = str(str2[i])                    #定义字符x
  7.     y = str(str2[i-3:i])                #定义字符x的前三个字符
  8.     z = str(str2[i+1:i+4])              #定义字符x的后三个字符
  9.     q = y+z
  10.     a = str(str2[i-4])                  #定义字符x前面第四个字符
  11.     b = str(str2[i+4])                  #定义字符x后面第四个字符
  12.     if x.islower() == True and q.isupper() == True and '\n' not in str(q) and \
  13.        a.isupper() == False and b.isupper() == False:
  14.         print(x,end='')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-1-11 15:29:35 | 显示全部楼层
本帖最后由 alltolove 于 2017-1-11 15:34 编辑

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-1-11 15:44:22 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-3-24 17:15:58 | 显示全部楼层    本楼为最佳答案   
我最近也被这题困扰了很久,问题和你一样,也是多了俩个字母,最后幸亏得到了大神的点播
我看了下你的代码,和我出现的问题基本一样,都是因为你在判断小写字母前面和后面三个字符是否为大写的时候,忽略了换行符‘\n’在前后三个字符里的情况,你可以在a被定义后加上一行:
  1. a = a.replace('\n','')
复制代码


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

  1.     if a[temp[i1]+1].isupper() == True     \
  2.        and a[temp[i1]+2].isupper() == True \
  3.        and a[temp[i1]+3].isupper() == True \
  4.        and a[temp[i1]-1].isupper() == True \
  5.        and a[temp[i1]-2].isupper() == True \
  6.        and a[temp[i1]-3].isupper() == True \
  7.        and a[temp[i1]-4].islower() == True  \
  8.        and a[temp[i1]+4].islower() == True:
复制代码


这样也就可以了

谢谢!单纯是想巩固自己学的内容,表达错误或者不好的地方忘谅解,另 我自己写的代码是这样的:
  1. str2 = '粘贴字符'
  2. #str2 = str2.replace('\n', '')
  3. lenght = len(str2)
  4. for i in range(3,lenght-4):
  5.          
  6.     x = str(str2[i])                    #定义字符x
  7.     y = str(str2[i-3:i])                #定义字符x的前三个字符
  8.     z = str(str2[i+1:i+4])              #定义字符x的后三个字符
  9.     q = y+z
  10.     a = str(str2[i-4])                  #定义字符x前面第四个字符
  11.     b = str(str2[i+4])                  #定义字符x后面第四个字符
  12.     if x.islower() == True and q.isupper() == True and '\n' not in str(q) and \
  13.        a.isupper() == False and b.isupper() == False:
  14.         print(x,end='')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-17 11:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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