鱼C论坛

 找回密码
 立即注册
查看: 2688|回复: 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需要传输数字。
>>> s = '1234'

>>> s[3]
'4'
>>> s[4]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    s[4]
IndexError: string index out of range
>>> 
>>> int('C')
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    int('C')
ValueError: invalid literal for int() with base 10: 'C'
>>> 
想知道小甲鱼最近在做啥?请访问 -> 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需要传输数字。
>>> s = '1234'

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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