鱼C论坛

 找回密码
 立即注册
查看: 2180|回复: 4

[已解决]字符串求助

[复制链接]
发表于 2022-8-5 23:35:36 | 显示全部楼层 |阅读模式

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

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

x
实在是不懂他给我报错报的什么
屏幕截图 2022-08-05 233137.png
屏幕截图 2022-08-05 233252.png
两个都不明白
最佳答案
2022-8-6 00:10:24
1. string index out of range,是指用index获取字符串中的字符时,index超出了它的最大长度。
2. invalid literal for int() with base 10: 'C', 是指给int传入了字符,int需要传输数字。
  1. >>> s = '1234'

  2. >>> s[3]
  3. '4'
  4. >>> s[4]
  5. Traceback (most recent call last):
  6.   File "<pyshell#2>", line 1, in <module>
  7.     s[4]
  8. IndexError: string index out of range
  9. >>>
  10. >>> int('C')
  11. Traceback (most recent call last):
  12.   File "<pyshell#3>", line 1, in <module>
  13.     int('C')
  14. ValueError: invalid literal for int() with base 10: 'C'
  15. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-6 00:10:24 | 显示全部楼层    本楼为最佳答案   
1. string index out of range,是指用index获取字符串中的字符时,index超出了它的最大长度。
2. invalid literal for int() with base 10: 'C', 是指给int传入了字符,int需要传输数字。
  1. >>> s = '1234'

  2. >>> s[3]
  3. '4'
  4. >>> s[4]
  5. Traceback (most recent call last):
  6.   File "<pyshell#2>", line 1, in <module>
  7.     s[4]
  8. IndexError: string index out of range
  9. >>>
  10. >>> int('C')
  11. Traceback (most recent call last):
  12.   File "<pyshell#3>", line 1, in <module>
  13.     int('C')
  14. ValueError: invalid literal for int() with base 10: 'C'
  15. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-6 00:59:27 | 显示全部楼层
想象一下,
当程序在处理最后一个字符时候,要和最后一个字符再后面的字符做比较(超出范围了)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-6 10:01:14 | 显示全部楼层
很好
  。别灰心
        继续加油!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-6 14:08:11 | 显示全部楼层
本帖最后由 hveagle 于 2022-8-6 14:10 编辑
  1. >>> s = '1234567'
  2. >>> #下标:0123456
  3. >>> s[6] #是7
  4. '7'
  5. >>> s[7] #超出范围啦
  6. Traceback (most recent call last):
  7.   File "<pyshell#3>", line 1, in <module>
  8.     s[7] #超出范围啦
  9. IndexError: string index out of range
复制代码

下一个:int()的问题
我们知道int()是可以把字符串转化成整数的,遇到字符串内容不是整数就会报错---->小甲鱼亲自翻译的中文版int()函数文档int() -- BIF [url]https://fishc.com.cn/thread-144631-1-1.html
[/url]
  1. ints = int(s)
  2. >>> ints
  3. 1234567
  4. >>> t = 'FishC'
  5. >>> intt = int(t)
  6. Traceback (most recent call last):
  7.   File "<pyshell#9>", line 1, in <module>
  8.     intt = int(t)
  9. ValueError: invalid literal for int() with base 10: 'FishC'
  10. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 05:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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