Click here to Skip to main content
14,873,468 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
Error code:

  if lstAns[i]==correct[i]:
TypeError: 'function' object is not subscriptable

Full code:

CorrectAnswer= ['B','D','A','A','B','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A']

def BDAABABACDBCDADCCBDA():

SA=open("StudentAnswers.txt","w")

SA.write("Answers are\n".format(BDAABABACDBCDADCCBDA))

SA.close()

def lstAns():

lstAns=list(SA.read())

correct=0;

incorrect=0;

for i in range(20):

if lstAns[i]==correct[i]:

correct+=1

elif lstAns[i] != correct[i]:

incorrect+=1

print("Correct answers: ",correct)

print("Incorrect answers: ",incorrect)

print("Student answer: ")

for i in range(20):

print(i+1,userlist[i])


What I have tried:

I don't know what to try with this error code every time I look up solutions to the error code I cant figure out how to apply them to the specific code above
Posted
Updated 15-Apr-21 20:12pm
v2

That's a name clash. You are using the same identifier, lstAns for both a function and a variable (a list).
   
You can't index a single value, only arrays.
Python
lstAns=list(SA.read())
correct=0;
...
if lstAns[i]==correct[i]:
   ...
lstAns is an array, correct isn't.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900