Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

I am supposed to:

  • Write the program to input the list of names of students. Display names of those students whose names end with the letter 'A'

so I have written the below code:

input_variable =input('Enter Your list : ')

list1=eval(input_variable)

i=0

while i<len(list1):

  a=list[i]

  if a[-1]=="a":

    print (list1[i])

I am getting the below error:

Traceback (most recent call last):

  File "main.py", line 5 in <module>

    a=list[i]

Type error: 'type' object is not subscribable

1 Answer

0 votes
by (36.8k points)
edited by

The correct code is:

 input_variable =input('Enter Your list : ')

list1=eval(input_variable)

i=0

while i<len(list1):

  a=list1[i]

  if a[-1]=="a":

    print (list1[i])

  i+=1

as you can see i added the "i+=1" and changed list to list1. 

Reason :

your code is not working because you didn't add the i+=1 so your loop has no end.

Improve your knowledge in data science from scratch using Data science online courses

Browse Categories

...