. Advertisement .
..3..
. Advertisement .
..4..
I get: ”’int’ object is not subscriptable” error when I am running this program:
# Sort code and Account number input
bank_details = int(input("Enter your sort code and account number: "))
# Get sort code using slicing
sort_code = bank_details[0:5]
# Get account number using slicing
account_number = [5:]
#print two separate values
print('Sort Code: ', sort_code)
print('Account number: ', account_number)
Enter your sort code and account number: 00000012345678
Then I receive a warning message:
TypeError Traceback (most recent call last)
1 sort_code=bank_details[0:5]
TypeError: 'int' object is not subscriptable
I am very urgent because this is my final exam question. Please help me!!!
The cause:
This error happens due to the command
sort_code = bank_details[0:5]
. You’re attempting to get the sort code from the bank details integer value by utilizing the unlawful subscriptable operator [0:5]. Therefore, the error appears.Solution:
You can fix this error by removing the
int()
function from theinput()
method, it will result in the bank_ details returned as a subscriptable string object. Then you can get the sort code and account number by indexing the bank_details string.