ViscloDev
ViscloDev

Reputation: 19

How can I select a char from a string inside a list in python and compare it with a single char?

Here is the code where I try to take the "t" char from the "two" string from the li list and compare it to a single "t" char:

li=["one","two","three"];

choice=1;

if li[choice[1]]=="t":
    print("valid");

else:
    print("unvalid");

The problem is that when I run it, it says:

Traceback (most recent call last):
  File "C:\Users\Yobob\Documents\cours\ISN\ProjetISN\testPython.py", line 3, in <module>
    if li[choice[1]]=="t":
TypeError: 'int' object is not subscriptable

Upvotes: 0

Views: 917

Answers (1)

blueteeth
blueteeth

Reputation: 3565

choice[1] is doing 1[1] which doesn't make sense. I think you want li[choice][1]. i.e.

if li[choice][1] == "t":

Upvotes: 1

Related Questions