-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.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
Kyran Oliver
  • 3
  • 1
  • 3
  • 2
    What do you think that `[i]` there does? – Tim Sep 21 '15 at 11:04
  • 1
    *"Need Help Fast !!!"* - this is **not** a helpdesk, please see http://meta.stackexchange.com/q/6506/248731 – jonrsharpe Sep 21 '15 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. – tripleee Sep 21 '15 at 11:08

2 Answers2

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.

Cyrbil
  • 6,341
  • 1
  • 24
  • 40
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.

rlinden
  • 2,053
  • 1
  • 12
  • 13