鱼C论坛

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

[已解决]在第19课的动动手第一题中有“tuple out of index range”的问题

[复制链接]
发表于 2017-4-20 19:55:55 | 显示全部楼层 |阅读模式

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

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

x
(题目的截图在附件里,不知道怎么放图片就只能放附件了)
目的就是输入一句话,然后让count()函数能够知道一句话里面有几个字母数字空格这样。
结果可以成功run出来的,但是会报错
“line 8, in count
    for i in c[ze]:
IndexError: tuple index out of range”
像这样。。不知道为啥?

  1. def count(*c):
  2.     k=len(c)
  3.     ind=0
  4.     al=['a',b'','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
  5.     nu=['0','1','2','3','4','5','6','7','8','9']
  6.     while ind<=k:
  7.         alpha,digit,space,othe=0,0,0,0
  8.         for i in c[ind]:
  9.             if i in al:
  10.                 alpha+=1
  11.                 continue
  12.             if i in nu:
  13.                 digit+=1
  14.                 continue
  15.             if i==' ':
  16.                 space+=1
  17.                 continue
  18.             else:
  19.                 othe+=1
  20.         ind+=1
  21.         print("第%d个字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d个。" %(ind,alpha,digit,space,othe))
复制代码
最佳答案
2017-4-20 22:09:24
看下面这个例子,你应该能找出你程序中哪里出错了吧
  1. >>> a = 'qwe'
  2. >>> len(a)
  3. 3
  4. >>> a[0]
  5. 'q'
  6. >>> a[1]
  7. 'w'
  8. >>> a[2]
  9. 'e'
  10. >>> a[3]
  11. Traceback (most recent call last):
  12.   File "<pyshell#5>", line 1, in <module>
  13.     a[3]
  14. IndexError: string index out of range
复制代码
q.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-20 20:03:37 | 显示全部楼层
ind不能等于k,元组的最后一个索引值等于长度-1

评分

参与人数 1鱼币 +5 收起 理由
下雨天出不去 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-20 22:09:24 | 显示全部楼层    本楼为最佳答案   
看下面这个例子,你应该能找出你程序中哪里出错了吧
  1. >>> a = 'qwe'
  2. >>> len(a)
  3. 3
  4. >>> a[0]
  5. 'q'
  6. >>> a[1]
  7. 'w'
  8. >>> a[2]
  9. 'e'
  10. >>> a[3]
  11. Traceback (most recent call last):
  12.   File "<pyshell#5>", line 1, in <module>
  13.     a[3]
  14. IndexError: string index out of range
复制代码

评分

参与人数 1鱼币 +5 收起 理由
下雨天出不去 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-21 23:49:52 | 显示全部楼层
ooxx7788 发表于 2017-4-20 20:03
ind不能等于k,元组的最后一个索引值等于长度-1

谢谢你!!你讲的很简洁!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 18:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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