为什么我会收到“TypeError: ‘int’ object is not subscriptable”?

时间:2021-06-17 05:06:49

标签: python python-3.x

import math
import location
if __name__ == "__main__" :
    locationlist = []
    while True :
        try : 
            coordinate = input("Enter X and Y separated by a space, or enter a non-number to stop: ")
            x,y = coordinate.split()
            x = int(x)
            y = int(y)
            locationlist.append(coordinate)
        except ValueError :
            break
    print("Points: ",locationlist)
    location.where_is_xy(coordinate,locationlist)
    
if(len(locationlist)>=2):
    print("Distances:")
    for i in range(0,len(locationlist)-1):
        a=list(locationlist[i])
        b=list(locationlist[i+1])
        distance=math.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)
        print(str(a)+" "+str(b)+" = "+str("%.2f" % round(distance, 2)))

^ 这是我的主要内容

def where_is_xy(coordinate,locationlist) :
    for coordinate in locationlist :
        templist = coordinate
        x,y = coordinate.split()
        x = int(x)
        y = int(y)
        if x != 0 :
            if y == 0 :
                Point = 'X-Axis.'
            elif x > 0 :
                if y > 0 :
                    Point = 'Quadrant 1.'
                elif y < 0 :
                    Point = 'Quadrant 4.'
            elif x < 0 :
                if y > 0 :
                    Point = 'Quadrant 2.'
                elif y < 0 :
                    Point = 'Quadrant 3.'
        elif x == 0 :
            if y != 0 :
                Point = 'Y-Axis.'
            elif y == 0 :
                Point = 'Origin.'
        print("(",x,",",y,") is ",Point)
        templist(zip(1, 1[1:]))
        print(templist)

^ 这是我的位置.py 此函数的输出如下:

runfile('C:/Users/aidan/Documents/CS410P/Labs/9L/main.py', wdir='C:/Users/aidan/Documents/CS410P/Labs/9L')
Reloaded modules: location

Enter X and Y separated by a space, or enter a non-number to stop: 1 1

Enter X and Y separated by a space, or enter a non-number to stop: -1 -1

Enter X and Y separated by a space, or enter a non-number to stop: -1 1

Enter X and Y separated by a space, or enter a non-number to stop: 1 -1

Enter X and Y separated by a space, or enter a non-number to stop: 
Points:  ['1 1', '-1 -1', '-1 1', '1 -1']
( 1 , 1 ) is  Quadrant 1.
Traceback (most recent call last):

  File "C:\Users\aidan\Documents\CS410P\Labs\9L\main.py", line 22, in <module>
    location.where_is_xy(coordinate,locationlist)

  File "C:\Users\aidan\Documents\CS410P\Labs\9L\location.py", line 35, in where_is_xy
    templist(zip(1, 1[1:]))

TypeError: 'int' object is not subscriptable

我不知道是什么导致了这个错误,任何帮助表示赞赏。我以为我的代码可以正常工作,但我收到了这个错误。我特别困惑,因为连 35 行代码都没有,只有 34 行 *我的代码从 8 行开始。

1 个答案:

答案 0 :(得分:1)

templist(zip(1, 1[1:]))

正如评论者所指出的,这是无用的。我删除了它,问题得到了解决。