I have a weird problem that I can't seem to understand. I'm writing a Python program that generates some random names from the many names previously given. For some reason, after running the program many times, the program returns a string index error. It doesn't for one iteration but does after many. I don't understand why. Here is my program:
class Random:
def __init__(self,seed):
self.seed = seed
self.curr = 0
def Next(self, Range):
if self.curr == 0:
rand = (16807*self.seed)%((2147483648)-1)
self.curr = rand
return rand % Range
else:
rand = (16807*self.curr)%((2147483648)-1)
self.curr = rand
return rand % Range
def choose(self, characters):
index = self.Next(len(characters))
return str (characters[index])
class Words:
def __init__(self, seed):
self.__first = ""
self.__follow = {"":""}
self.__random = Random(seed)
def add(self, word):
self.__first += word[0]
a = 0
while a < (len(word) - 1):
if word[a] in self.__follow:
self.__follow[word[a]] += word[a+1]
a+=1
else:
self.__follow[word[a]] = word[a+1]
a+=1
return None
def make(self,size):
self.example = ""
self.example += self.__random.choose(self.__first)
a = 1
while a < size:
if (self.__follow).has_key(self.example[a-1]):
self.example += self.__random.choose(self.__follow[self.example[a-1]])
a += 1
else:
self.example = self.__random.choose(self.__first)
a += 1
return self.example
prez = Words(101)
prez.add('Washington')
prez.add('Adams')
prez.add('Jefferson')
prez.add('Madison')
prez.add('Monroe')
prez.add('Adams')
prez.add('Jackson')
prez.add('Vanburen')
prez.add('Harrison')
prez.add('Tyler')
prez.add('Polk')
prez.add('Taylor')
prez.add('Fillmore')
prez.add('Pierce')
prez.add('Buchanan')
prez.add('Lincoln')
prez.add('Johnson')
prez.add('Grant')
prez.add('Hayes')
prez.add('Garfield')
prez.add('Arthur')
prez.add('Cleveland')
prez.add('Harrison')
prez.add('Cleveland')
prez.add('Mckinley')
prez.add('Roosevelt')
prez.add('Taft')
prez.add('Wilson')
prez.add('Harding')
prez.add('Coolidge')
prez.add('Hoover')
prez.add('Roosevelt')
prez.add('Truman')
prez.add('Eisenhower')
prez.add('Kennedy')
prez.add('Johnson')
prez.add('Nixon')
prez.add('Ford')
prez.add('Carter')
prez.add('Reagan')
prez.add('Bush')
prez.add('Clinton')
prez.add('Bush')
prez.add('Obama')
prez.add('Trump')
And here is my execution trace:
>>> k = 100
>>> while k > 0:
print prez.make(7)
k -= 1
Grurumo
Jonsone
Hansoro
Cldisoe
Fovenay
Fisooos
Clnnhin
Taruson
Tayeylo
Adgeama
Madiela
Roshnbu
Obayeay
Ponhuso
Monconc
Mcesens
Haringe
Cosovel
Lingten
Jarrixo
Jonarfi
Adinhum
Ponlele
Polnlel
Ninasoo
Clannga
Relayle
Traceback (most recent call last):
File "<pyshell#4>", line 2, in <module>
print prez.make(7)
File "C:\Python27\1913_project1_limxx518.py", line 43, in make
if (self.__follow).has_key(self.example[a-1]):
IndexError: string index out of range
I have looked through line 43 and I don't seem to find any problem. Since Python can have integers of any size, I don't think it's the issue of numbers getting too big. Also, self.example is just a word of 7 letters, so I shouldn't have any issue there. I just can't find a reason. Why does this error occur only after many iterations?
Aucun commentaire:
Enregistrer un commentaire