98没有K 发表于 2022-8-5 23:35:36

字符串求助

实在是不懂他给我报错报的什么


两个都不明白

suchocolate 发表于 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
'4'
>>> s
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
    s
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'
>>>

liuzhengyuan 发表于 2022-8-6 00:59:27

想象一下,
当程序在处理最后一个字符时候,要和最后一个字符再后面的字符做比较(超出范围了)

小峰新手 发表于 2022-8-6 10:01:14

很好
。别灰心
      继续加油!!!!

hveagle 发表于 2022-8-6 14:08:11

本帖最后由 hveagle 于 2022-8-6 14:10 编辑

>>> s = '1234567'
>>> #下标:0123456
>>> s #是7
'7'
>>> s #超出范围啦
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
    s #超出范围啦
IndexError: string index out of range
下一个:int()的问题
我们知道int()是可以把字符串转化成整数的,遇到字符串内容不是整数就会报错---->小甲鱼亲自翻译的中文版int()函数文档int() -- BIF https://fishc.com.cn/thread-144631-1-1.html

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'
>>>
页: [1]
查看完整版本: 字符串求助