Home > Back-end >  'int' object is not subscriptable python error
'int' object is not subscriptable python error

Time:09-28

My problem is error that you can see abowe. Here is my code. I want to check if n1 spelled backwards is equal to n2. Error is in line 6. I tried to user [::-1] but i think my knowledge about this is to small to use it well. Can you help?

import string
n1 = int(input("Your first number: "))
n2 = int(input("Your second number: "))
str(n1)
str(n2)
if n1 == n2[::-1]:
    print("numbers are mirrored")
else:
    print("numbers are not mirrored")

CodePudding user response:

The input(...) function returns a str value, so if you wanted you could have code like:

n1 = input("Your first number: ")
n2 = input("Your second number: ")

print(f'numbers are {"not " if n1 != n2[::-1] else ""}mirrored')

Out:

Your first number: 123
Your second number: 321
numbers are mirrored

CodePudding user response:

the whole problem is switching them from int to string, try getting rid of that code:

import string
n1 = input("Your first number: ")
n2 = input("Your second number: ")

if n1 == n2[::-1]:
    print("numbers are mirrored")
else:
    print("numbers are not mirrored")
  • Related