i need some help with this code please :)
when i run this code the output after each time the nested loop runs should look like this:
{'gender': 'Female', 'age': 9, 'fur': 'Brown', 'name': 'Savannah'}
{'gender': 'Male', 'age': 9, 'fur': 'White', 'name': 'Thumper'}
{'gender': 'Male', 'age': 8, 'fur': 'Brown', 'name': 'Ian'}
{'gender': 'Female', 'age': 8, 'fur': 'Brown', 'name': 'Natalie'}
{'gender': 'Male', 'age': 7, 'fur': 'White', 'name': 'Sebastian'}
with the bunnies ordered by age
however what is happening is that after the initial 5 bunnies created are deleted
the 6th bunny seems to be removed from the dict for one loop and it looks like this:
{'gender': 'Female', 'age': 9, 'fur': 'Brown', 'name': 'Savannah'}
{'gender': 'Male', 'age': 9, 'fur': 'White', 'name': 'Thumper'}
{'gender': 'Female', 'age': 8, 'fur': 'Brown', 'name': 'Natalie'}
{'gender': 'Male', 'age': 7, 'fur': 'White', 'name': 'Sebastian'}
the issue is that while its not in the dict the age of the key wont increase so on the next loop it looks like this:
{'gender': 'Male', 'age': 9, 'fur': 'Brown', 'name': 'Ian'}
{'gender': 'Female', 'age': 8, 'fur': 'Brown', 'name': 'Natalie'}
{'gender': 'Male', 'age': 8, 'fur': 'White', 'name': 'Sebastian'}
instead of this:
{'gender': 'Male', 'age': 9, 'fur': 'Brown', 'name': 'Ian'}
{'gender': 'Female', 'age': 9, 'fur': 'Brown', 'name': 'Natalie'}
{'gender': 'Male', 'age': 8, 'fur': 'White', 'name': 'Sebastian'}
what i need help with if finding out where that dict key is going and why.
thanks for the help :)
P.S
sorry if the code isn't in the blue code box i kept getting this msg
"You do not have permission to create tags. You may only use existing tags."
so if anyone knows anything about that you can post that too :) unless my code is formatted properly afterall
import random
FemaleBunnyName = open("BunnyFemaleName.txt").read().strip().split(".")
MaleBunnyName = open("BunnyMaleName.txt").read().strip().split(".")
del FemaleBunnyName[-1]
del MaleBunnyName[-1]
i=0
MaleBunnyPopulation=0
FemaleBunnyPopulation=0
FemaleBreedingAge=0
BunnyDict={}
BunnyKey=[]
class Bunny:
def _init_(self,Name,Fur,Gender,Age):
self.Name = Name
self.Fur = Fur
self.Gender = Gender
self.Age = Age
def BunnyFur(self):
Fur = open("BunnyFur.txt").read().strip('\n').split(".")
del Fur[-1]
self.Fur=Fur[random.randrange(0,3)]
return self.Fur
def BunnyName(self,Name):
global FemaleBunnyName
global MaleBunnyName
self.Name=Name
if self.Name=='Male':
vv=random.randrange(0,len(MaleBunnyName))
self.Name=MaleBunnyName[vv]
del MaleBunnyName[vv]
return self.Name
elif self.Name=='Female':
vv=random.randrange(0,len(FemaleBunnyName))
self.Name=FemaleBunnyName[vv]
del FemaleBunnyName[vv]
return self.Name
def BunnyGender(self):
self.Gender = random.randrange(0,2)
if self.Gender == True:
return 'Male'
else:
return 'Female'
def BunnyAgeIncrement(x):
global FemaleBunnyPopulation
global MaleBunnyPopulation
global BunnyDict
i=0
while i<x:
age=0
BunnyFur=B.BunnyFur()
BunnyGender=B.BunnyGender()
BunnyName=B.BunnyName(BunnyGender)
BunnyKey.append(BunnyName)
i+=1
BunnyDict.update({BunnyName:{'age':age,'gender':BunnyGender,'fur':BunnyFur,'name':BunnyName}})
if BunnyGender=='Male':
MaleBunnyPopulation+=1
elif BunnyGender=='Female':
FemaleBunnyPopulation+=1
return BunnyDict
B=Bunny()
BunnyAgeIncrement(5)
while MaleBunnyPopulation>0 and FemaleBunnyPopulation>0 and MaleBunnyPopulation<50 and FemaleBunnyPopulation<50 :
a = len(BunnyKey)
i=0
print '\n'
while i<a:
if BunnyDict[BunnyKey[i]]['age']<10:
print BunnyDict[BunnyKey[i]]
BunnyDict[BunnyKey[i]]['age'] +=1
if BunnyDict[BunnyKey[i]]['age']>2 and BunnyDict[BunnyKey[i]]['gender']=='Female' and MaleBunnyPopulation>0:
FemaleBreedingAge+=1
i+=1
elif BunnyDict[BunnyKey[i]]['age']==10:
if BunnyDict[BunnyKey[i]]['gender']=='Male':
MaleBunnyPopulation-=1
MaleBunnyName.append(BunnyDict[BunnyKey[i]]['name'])
elif BunnyDict[BunnyKey[i]]['gender']=='Female':
FemaleBunnyPopulation-=1
FemaleBunnyName.append(BunnyDict[BunnyKey[i]]['name'])
a-=1
del BunnyDict[BunnyKey[i]]
del BunnyKey[i]
i+=1
BunnyAgeIncrement(FemaleBreedingAge)
FemaleBreedingAge = 0
print FemaleBunnyPopulation
print len(FemaleBunnyName)
print MaleBunnyPopulation
print len(MaleBunnyName)
print 'done'