0

So I've gotten mostly everything to work for my python project except for this. I keep getting this error

builtins.IndexError: list index out of range

for line 10 which is asterixed to show the location of the problem.

i = 0
**NumberOfHorses=int(sys.argv[1])**
while i!=NumberOfHorses:

and I'm not sure how to fix it or what is wrong. Any help? Here is the full code.

from random import randint
import sys
horse = {}
randNumber={}
timeTaken={}
currentPosition={}
totalTime = {}

i = 0
NumberOfHorses=int(sys.argv[1])
while i!=NumberOfHorses:
    horse[i]=0
    timeTaken[i]=0
    currentPosition[i]=0
    i+=1
    check=0
    i=0
    totalMiles = 10560
    while(1):
        for i in range(0,NumberOfHorses):
            currentPosition[i] = currentPosition[i] + randint(4,41)
            if(currentPosition[i]>= 10560):
                check=1
                break
            timeTaken[i]+=1
            if(check==1):
                break
            print ('race is finished with following details:')
            for i in range(0,NumberOfHorses):
                print ('horse no.',i, 'distance covered-',currentPosition[i],'in time ',timeTaken[i])

Thanks in advance.

Dirk
  • 3,030
  • 1
  • 34
  • 40
  • Python is expecting that your going to pass in a command line argument to your script. – Christian Dean May 06 '17 at 05:40
  • `sys.argv` is a list of common-line arguments, the first element, `sys.argv[0]` is the program name, so obviously you are not providing a command-line argument when you run it. – cdarke May 06 '17 at 05:40

0 Answers0