Last active
December 22, 2017 22:15
-
-
Save Xetera/3418d3f15c43ff52f4602a4ebf3f99d6 to your computer and use it in GitHub Desktop.
Python Teaching Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Building: | |
def __init__(self, world, name, owner, color, _type): | |
self.name = name | |
self.owner = owner # Person obj | |
self.color = color | |
self.type = _type | |
self.assign_person(owner) | |
@property | |
def ID(self): | |
# unlikely to assign the same ID # | |
return random.randrange(100000, 999999) | |
def assign_person(self, person): | |
person.add_housing(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
male_names = ["James", "John", "Alan"] | |
female_names = ["Alice", "Christina", "Elizabeth"] | |
genders = ["Male", "Female"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import CONSTANTS | |
class World: | |
def __init__(self, world_name): | |
self.name = world_name | |
self.people = [] | |
self.buildings = [] | |
def add_person(self, person): | |
self.people.append(person) | |
return self.people | |
def population(self): | |
return len(self.people) | |
def get_people(self): | |
all_people = [] | |
for i in self.people: | |
all_people.append(i.__dict__) | |
return all_people | |
def most_common_names(self): | |
names = {"Alan": 0, "James": 0, "John":0, "Alice":0, "Christina":0 , "Elizabeth":0} | |
for i in self.people: | |
names[i.name] += 1 | |
return names | |
#================================================# | |
class Person: | |
def __init__(self, world, age, name=None, gender=None): | |
if gender: | |
self.gender = gender | |
else: | |
self.gender = Person.generate_gender() | |
if name: | |
self.name = name | |
else: | |
self.name = Person.generate_name(self.gender) | |
self.age = age | |
self.owned = [] | |
world.add_person(self) | |
@staticmethod | |
def generate_gender(): | |
index = random.randrange(2) | |
gender_selection = CONSTANTS.genders[index] | |
return gender_selection | |
@staticmethod | |
def generate_name(gender=None): | |
""" | |
returns gender based names | |
""" | |
if gender == "Male": | |
names = CONSTANTS.male_names | |
elif gender == "Female": | |
names = CONSTANTS.female_names | |
elif not gender: | |
names = CONSTANST.male_names + CONSTANTS.female_names | |
else: | |
raise Exception("{} is not a valid gender".format(gender)) | |
return random.choice(names) | |
def add_housing(self, housing): | |
housing_info = {} | |
print(dir(housing)) | |
# self.owned[housing.id] = | |
def get_age(self): | |
return self.my_age | |
def set_name(self, new_name): | |
self.name = new_name | |
return "Your new name is {}".format(self.name) | |
earth = World("Earth") | |
for i in range(1000): | |
person = Person(earth, 20) | |
print("Earth has a population of {}".format(earth.population())) | |
print(earth.most_common_names()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment