鱼C论坛

 找回密码
 立即注册
查看: 2575|回复: 10

零基础Python 020 动动手 1 我的方法为什么不行

[复制链接]
发表于 2016-11-29 15:26:06 | 显示全部楼层 |阅读模式
8鱼币
  1. temp = input('请输入字符串:')
  2. for i in temp:
  3.     n = temp.find(i)
  4.     if i.islower() and n > 3 and temp[n-1].isupper() and temp [n-2].isupper() and temp [n-3].isupper() and (not temp[n-4].isupper()) and temp[n+1].isupper() and temp[n+2].isupper() and temp[n+3].isupper() and (not temp[n+4].isupper()):
  5.         print(i,end = '')
  6.     elif i.islower() and n == 3 and temp[2].isupper() and temp [1].isupper() and temp [0].isupper() and temp[4].isupper() and temp[5].isupper() and temp[6].isupper() and (not temp[7].isupper()):
  7.         print(i,end = '')
复制代码

最佳答案

查看完整内容

因为你的第2个a的n=temp.find(a)去的值是第一个a的值,也就是3所以可以输出 请输入字符串:DDDaDDDbDDDc abTraceback (most recent call last): File "F:\py3.5\1.py", line 4, in if i.islower() and n > 3 and temp[n-1].isupper() and temp [n-2].isupper() and temp [n-3].isupper() and (not temp[n-4].isupper()) and temp[n+1].isupper() and temp[n+2].isupper() and temp[n+3].isupper() and (not temp[n+4] ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-29 15:26:07 | 显示全部楼层
超人啥啥曼 发表于 2016-11-30 16:27
这个什么情况?我没有改我的代码

因为你的第2个a的n=temp.find(a)去的值是第一个a的值,也就是3所以可以输出
请输入字符串:DDDaDDDbDDDc
abTraceback (most recent call last):
  File "F:\py3.5\1.py", line 4, in <module>
    if i.islower() and n > 3 and temp[n-1].isupper() and temp [n-2].isupper() and temp [n-3].isupper() and (not temp[n-4].isupper()) and temp[n+1].isupper() and temp[n+2].isupper() and temp[n+3].isupper() and (not temp[n+4].isupper()):
IndexError: string index out of range
如果输入以上字符串就会报错,因为取到c的时候n+2以及类似的就超出了字符串的范围就会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-29 15:57:19 | 显示全部楼层
n = temp.find(i)这个语句n只会等于字母i最开始的位置,不会是每次都出现的位置,不止这一个问题,if后面嵌套那么多条件而且还不是一个整体条件,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-29 17:10:38 | 显示全部楼层
`(37E2DZSO8CMK{DJU@2X[2.png
这个什么原因啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-29 20:56:37 | 显示全部楼层
你写的这代码看起来很吃力,建议多练
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-30 16:21:06 | 显示全部楼层
Damn_it 发表于 2016-11-29 20:56
**** 作者被禁止或删除 内容自动屏蔽 ****

我这边显示你的回答是‘**** 作者被禁止或删除 内容自动屏蔽 ****’
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-30 16:27:31 | 显示全部楼层
小茗同学 发表于 2016-11-29 15:57
n = temp.find(i)这个语句n只会等于字母i最开始的位置,不会是每次都出现的位置,不止这一个问题,if后面 ...

这个什么情况?我没有改我的代码
170956a9ea1y3jzccjcbse.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-11-30 19:21:25 | 显示全部楼层
小茗同学 发表于 2016-11-30 16:59
因为你的第2个a的n=temp.find(a)去的值是第一个a的值,也就是3所以可以输出
请输入字符串:DDDaDDDbDDDc ...

“if后面嵌套那么多条件而且还不是一个整体条件”这句求详细说明,求指点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-1 14:26:47 | 显示全部楼层
超人啥啥曼 发表于 2016-11-30 19:21
“if后面嵌套那么多条件而且还不是一个整体条件”这句求详细说明,求指点

我之前以为必须用一个括号括起来才是整体的if条件,好像LZ那么写也是对的,抱歉啊,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-18 00:22:14 | 显示全部楼层
,被禁了十几天
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-18 18:56:44 | 显示全部楼层
看看视频
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-24 05:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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