what is wrong with my python code?

D

Dongsheng Ruan

I got feed back saying" list object is not callable". But I can't figure out
what is wrong with my code.

A=[3,5,4,9,6,7]
l=len(A)-1

for i in range(l):
print A(i)
 
P

Pierre GM

I got feed back saying" list object is not callable". But I can't figure
out what is wrong with my code.
for i in range(l):
print A(i)

You're calling A, when you want to access one of its elements: use the
straight brackets [

for i in range(l):
print A
 
B

Bruno Desthuilliers

Dongsheng Ruan a écrit :
I got feed back saying" list object is not callable". But I can't figure out
what is wrong with my code.
>
A=[3,5,4,9,6,7]
l=len(A)-1

for i in range(l):
print A(i)
The error message is quite clear when you remember that () is the call
operator. For the subscribe operator, you want [].

And FWIW, Python for loops are smarter than that:


A = [3,5,4,9,6,7]
for i in A:
print i
 
J

John Machin

I got feed back saying" list object is not callable". But I can't figure out
what is wrong with my code.

A=[3,5,4,9,6,7]
l=len(A)-1

for i in range(l):
print A(i)

What the others said, *plus*:

(1) you are subtracting 1 off the length, so after the change from ()
to [], the last item would not be printed.

(2) have you considered working through a tutorial?

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top