-3

I am trying to create a ballot system in which a user can input candidates names and then input preference scores. The program then works out the winner and prints a result.

I am getting an annoying return from my console when I run this line of code.

for i in range(len(winners)[i]):

The error reads:

TypeError: 'int' object is not subscriptable

(Edited: Code dump redacted.)

3
  • 2
    What do you think that [i] there does? Commented Sep 21, 2015 at 11:04
  • 1
    "Need Help Fast !!!" - this is not a helpdesk, please see meta.stackexchange.com/q/6506/248731 Commented Sep 21, 2015 at 11:08
  • 3
    @Wolf I removed it because the question is pretty clear without the full dump. You can fetch the code from the edit history if you are pathologically curious. Commented Sep 21, 2015 at 11:08

2 Answers 2

1

You want to iterate over winners:

for i in winners:

What you were trying to do::

for i in range(len(winners)):

will iterate through indexes, your error was the [i] that have nothing to do here.

Sign up to request clarification or add additional context in comments.

Comments

0

The function len returns an integer, which is the length of the array in question. Hence, when you add the subscript [i], you are trying to access an array, which you don't have.

If you want access to all indices, you need to use for i in range(0,len(winners)). If you want access to every element of the array, without worrying about the indices, the use for s in winners.

I hope it helps.

1 Comment

Note that 0 is the default start argument to range, so you can leave it out in this case

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.