Jack_P
Jack_P

Reputation: 49

Python 3x: IndexError - tuple out of range

I am having trouble with this project, which is functional immediately after coding, but then breaks as such in PyCharm:

Traceback (most recent call last):
line 38, in <module>
1

    exec("dict" + str(x) + "['Team'] = '" + Sponsors[randint(1, len(Sponsors))] + " " + Cars[randint(1, len(Cars))] + "'")
IndexError: tuple index out of range

How can I fix this error? Also, if it is a train-wreck, please tell me: I am a relative newcomer to Python, and may be going about things the long-winded way.

#RallyCarsProject

from random import randint
from random import shuffle
import time

#Tuples to create names
Cars = "Honda", "Subaru", "Vauxhall", "Peugeot", "Renault", "Mitsubishi", "Audi", "Mini", "BMW", "Fiat", "Nissan", "Ford", "Kia", "Hyundai", "Suzuki", "Volvo", "Toyota", "Citroen", "Mercedes", "Jaguar", "Mazda", "Infiniti", "Saturn", "Chevrolet", "Cadillac", "Pontiac", "Tesla", "Volkswagen", "Skoda"
Sponsors = "Halfords", "QuikFit", "Castrol", "Shell", "BP", "WD40", "GO Outdoors", "Dunlop", "Michelin", "Mobil 1", "Brembo", "Sparco", "Marlboro", "Esso", "Texaco", "Pirelli", "Gulf", "Blitz", "Motul", "K&N"
Fnames = "Kimberlee", "Jack", "Doyle", "Scotty", "Nichelle", "Kitty", "Fredia", "Van", "Erminia", "Cornell", "Chas", "Bennett", "Crystal", "Donny", "Florene", "Francesca", "Prince", "Johnsie", "Stepanie", "Adolfo", "Gilda", "Ike", "Lanelle", "Monserrate", "Erasmo", "Darci", "Refugia", "Clarine", "Alesia", "Luciano", "Genaro", "Valentin", "Howard", "Whitney", "Lou", "Roy", "Willis", "Dewayne", "Giuseppe", "Terence", "Rene", "Noe", "Gregory", "Al", "Earnest", "Lacy", "Michale", "Calvin", "Leandro", "Chung", "Lowell", "Keven", "Olen", "Tim", "Edmond", "Edmond", "Paul", "Truman", "Dario", "Mel", "Rickie", "Victor", "Rubin", "Shannon", "Zane", "Reid", "George", "Diego", "Denver", "Broderick", "Dwain", "Benny", "Chase", "Abram", "Terrell", "Wally", "Stephen", "Phil", "Lynn", "Major", "Marlin", "Tomas", "Zack", "Quentin", "Claudio", "Tad", "Virgilio", "Cornelius", "Karl", "Ian", "Carlos", "Clemente", "Theron", "Filiberto", "Galen", "Winfred", "Gilberto", "Porfirio", "Neal", "Doug", "Demarcus", "Shon", "Geoffrey", "Houston", "Isreal", "Rudolf", "Cecil", "Hipolito", "Martin", "Antwan", "Riley", "Domenic", "Bennie", "Chester", "Shannon", "Isaac", "Roman", "Billie", "Hyman", "Bradford", "Vernon", "Tyson", "Gordon", "Trevor", "Rhett", "Hobert", "Leif", "Alexander", "Maximo", "Adalberto", "Gary", "Rafael", "Freddy", "Joel", "Kieth", "Tad", "Mitchell", "Margarito", "Paul", "Palmer", "Mauro", "Sanford", "Darryl", "Alejandro", "Weldon", "Ahmed", "Federico"
Lnames = "Parkinson", "Russel", "Jenney", "Sapers", "Zalkalns", "Lally", "Zucker", "Quelch", "Devita", "Mora", "Defoe", "Garnett", "Rosado", "Heanue", "Ninon", "Galton", "Carlson", "Young", "Checa", "Mcneal", "Sjostrom", "Broderick", "Gabriels", "Koniaris", "Vines", "Shang", "Mishra", "Simons", "Mulford", "Rousseau", "Johnsen", "Cimbalo", "Sheridan", "Teisberg", "Grynkiewicz", "O'Neill", "Silver", "Glasman", "Goldizen", "Cupal", "Bernassola", "Corsini", "Soccorsi", "Barese", "Ouyang", "Schnur", "Cazden", "Wirka", "Cuccurullo", "Scanlon", "Lacount", "Lauterbach", "Wayland", "Devivo", "Price", "Tiffany", "Prestwich", "Orchard", "Elmore", "Plato", "Drew", "Davidian", "Vitali", "Aureli", "Seed", "Fujimoto", "Carlyle", "Leder", "Hallmann", "Bruzik", "Mchale", "Maruca", "Southey", "Crane", "Soon", "Strachan", "Velupillai", "Frommer", "Antonov", "Fortunato", "Vittadini", "Cushman", "Glasser", "Gelberman", "Leavitt", "Gwinn", "Knight", "Holyoke", "Davidsen", "Dynkin", "Giudici", "Alspach", "Kornai", "Ahumada", "Engelhardt", "Halliday", "Posner", "Self", "Schaefer", "Edelman", "Ramian", "Madore", "Royal", "Fearnley", "Heidbreder", "Bronson", "Kisiel", "Horgan", "Boatman", "Woolf", "Shea", "O'Connor", "Chipman", "Kotilainen", "Hanlon", "Hayden-Smith", "Bachelder", "Linehan", "Athanassakis", "Nagaran", "Pearce", "Gallucio", "Siner", "Jardetzky", "Blanchette", "Chiozzi", "Turrall", "Rothbard", "Mortera", "Joensen", "Tocqueville", "Ibarra", "Shlyakhter", "Strugnell", "Wolff", "Sheerr", "Tso", "Hogan", "Matsumura", "Keats", "Shechter", "Kokkinis", "Levenson", "Fiori", "Holleran", "Satoh", "Warfield", "Coleman", "Geisser", "Zaidi", "Holtzclaw", "Zapatka", "Stimpfle", "de Sena"
Stages = "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua & Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Rep", "Chad", "Chile", "China", "Colombia", "Comoros", "Congo", "Congo (Democratic Republic of)", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland (Republic of)", "Israel", "Italy", "Ivory Coast", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea North", "Korea South", "Kosovo", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar (Burma)", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russian Federation", "Rwanda", "St Kitts & Nevis", "St Lucia", "Saint Vincent & the Grenadines", "Samoa", "San Marino", "Sao Tome & Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"

#Intro and sleep for reading time
print("Welcome to the World Rally Championship!")
time.sleep(1)

#Create number of teams and races
teams = randint(10,16)
races = randint(5,12)
print("This year's competition features", str(teams), "teams competing over", str(races), "races.")

#Creates array, then appends the number of races' worth of array, inside which a tuple length number of competitors
#Shuffles list of competitors to determine positions
results = []
for x in range(1, (races + 1)):
    exec("Stage" + str(x) + "= []")
    for y in range(1, (teams + 1)):
        exec("Stage" + str(x) + ".append(" + str(y) + ")")
    exec("shuffle(Stage" + str(x) + ")")
    exec("results.append(Stage" + str(x) + ".copy())")

#Creates team names, driver and co-driver in storage array
names = []
for x in range(1, (teams + 1)):
    print(str(x))
    exec("dict" + str(x) + "= {}")
    exec("dict" + str(x) + "['Team'] = '" + Sponsors[randint(1, len(Sponsors))] + " " + Cars[randint(1, len(Cars))] + "'")
    exec("dict" + str(x) + "['Driver'] = '" + Fnames[randint(1, len(Sponsors))] + " " + Lnames[randint(1, len(Cars))] + "'")
    exec("dict" + str(x) + "['CoDriver'] = '" + Fnames[randint(1, len(Sponsors))] + " " + Lnames[randint(1, len(Cars))] + "'")
    exec("names.append(dict" + str(x) + ")")
    #Creates variables with iterated names
    exec("Team" + str(x + 1) + "= 0")

#Set scoring system
scores = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1]

#Show teams
for x in range(1, (teams + 1)):
    print("")
    print("Team", str(x) + ":", names[x-1]['Team'])
    print("Driver:", names[x-1]['Driver'])
    print("Co-Driver:", names[x-1]['CoDriver'])

Upvotes: 0

Views: 346

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121266

You are generating an index between 1 and the length of the tuple inclusive:

Sponsors[randint(1, len(Sponsors))]

Python indexes start at 0, not 1, and the last permissible index is the length minus 1, so exclusive, not inclusive. This is what causes your IndexError:

>>> Sponsors = "Halfords", "QuikFit", "Castrol", "Shell", "BP", "WD40", "GO Outdoors", "Dunlop", "Michelin", "Mobil 1", "Brembo", "Sparco", "Marlboro", "Esso", "Texaco", "Pirelli", "Gulf", "Blitz", "Motul", "K&N"
>>> Sponsors[len(Sponsors)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> Sponsors[len(Sponsors) - 1]
'K&N'

You'd have to use Sponsors[random.randrange(len(Sponsors))] instead, or much better, use random.choice(Sponsors):

>>> from random import choice
>>> choice(Sponsors)
'WD40'
>>> choice(Sponsors)
'Blitz'

which takes care of the boundaries for you.

Next, there is no need to use exec() or eval() in your code. Create a top level list to hold your teams:

teams = []

and add your dictionaries to that. That way you can just address teams[0] for the first team, etc., rather than generate names like dict1 and dict2.

Upvotes: 1

Related Questions