smallvt
smallvt

Reputation: 63

Why am I getting "TypeError: 'int' object is not subscriptable"?

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)))

^ This is my main

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)

^ this is my location.py The output for this function reads:

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

I do not know what causes this error, any help is appreciated. I thought my code was working, but instead I got this error. I am extra confused because there are not even 35 lines of code, only 34 *my code starts at 8.

Upvotes: 0

Views: 217

Answers (1)

smallvt
smallvt

Reputation: 63

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

As commenters have noted, this is useless. I removed this and the problem was fixed.

Upvotes: 1

Related Questions