Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 索引错误问题_Python 3.x_Index Error - Fatal编程技术网

Python 3.x 索引错误问题

Python 3.x 索引错误问题,python-3.x,index-error,Python 3.x,Index Error,所以我正在制作一个计算器,它接收一个字符串并检查它是否有某些单词,比如加法或减法,然后查找整数。但是,在我当前的代码中,我运行它并得到以下错误消息: Traceback (most recent call last): File "python", line 1, in <module> File "python", line 7, in calculator IndexError: string index out of range 如果有人能帮我,那就太好了!非

所以我正在制作一个计算器,它接收一个字符串并检查它是否有某些单词,比如加法或减法,然后查找整数。但是,在我当前的代码中,我运行它并得到以下错误消息:

Traceback (most recent call last):
    File "python", line 1, in <module>
    File "python", line 7, in calculator
IndexError: string index out of range

如果有人能帮我,那就太好了!非常感谢

我相信您的问题在于
类型(int(string[I+1]))

由于有一个
for
循环,
i
可能已经指向
字符串的最后一个索引。当您向其中添加
1
时,将得到一个
索引器

例如:

s = 'blabla'
for i in range(len(s)):
    print(s[i])
s = 'blabla'
for i in range(len(s)):
    print(s[i+1])
输出:

b
l
a
b
l
a
l
a
b
l
a

File "C:\Users\python\scratch\untitled-1.py", line 3, in <module>
  print(s[i+1])

builtins.IndexError: string index out of range
例如:

s = 'blabla'
for i in range(len(s)):
    print(s[i])
s = 'blabla'
for i in range(len(s)):
    print(s[i+1])
输出:

b
l
a
b
l
a
l
a
b
l
a

File "C:\Users\python\scratch\untitled-1.py", line 3, in <module>
  print(s[i+1])

builtins.IndexError: string index out of range
l
A.
B
L
A.
文件“C:\Users\python\scratch\untitled-1.py”,第3行,在
打印(s[i+1])
builtins.Indexer错误:字符串索引超出范围
与我的朋友(@Kay Ace Elits)坐下来,意识到有很多事情不对劲,但我们把这件事拼凑了起来

def calculator(string):
    if "add" in string:

        total = 0
        first_string = "" # before a in add
        second_string = "" # after d in add

        value_list = string.split('add')
        for number in value_list:
            total += int(number)

        print(total)

    elif  "Add" in string:
        total = 0
        first_string = ""
        second_string = ""

        value_list = string.split('Add')
        for number in value_list:
            total += int(number)

        print(total)

### our test your can modify for other factors
### like spellings and different operations
string = "22add43"
calculator(string)

你的凹痕破了,我该怎么修呢?我只是试着把它们全部重新输入。在任何编辑器中编写代码时都要这样做,然后选择它,然后按
CTRL+K
,这也不起作用:(当
I=0
时,会产生
string[-1]
哪个是字符串的最后一个元素…不确定是否需要。感谢@EdwinvanMierlo提出这一点,这是我的疏忽,我将在运行测试后编辑代码。缩进已关闭