Created
February 18, 2014 17:47
-
-
Save anonymous/9075932 to your computer and use it in GitHub Desktop.
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
# This code is released into the public domain. License: Creative Commons Zero (CC0) | |
# person.py | |
import datetime | |
class Person(object): | |
forenames = None | |
surname = None | |
maiden_name = None | |
date_of_birth = None | |
place_of_birth = None | |
interests = [] | |
def describe_age(self): | |
age = self.calculate_current_age() | |
if age < 13: | |
return "child" | |
elif age < 20: | |
return "teenager" | |
else: | |
return "adult" | |
def add_interest(self, interest): | |
self.interests.append(interest) | |
def calculate_current_age(self): | |
return (datetime.datetime.now() - self.date_of_birth).days / 365 | |
# commandline.py | |
import sys | |
import datetime | |
def input_date(question): | |
date_str = input(question + " (DD/MM/YYYY) ") | |
return datetime.datetime.strptime(date_str, "%d/%m/%Y") | |
def set_basics(): | |
global person | |
person.forenames = input("Forename(s): ") | |
person.surname = input("Surname: ") | |
maiden_name = input("Maiden Name: ") | |
if len(maiden_name) > 0: | |
person.maiden_name = maiden_name | |
person.date_of_birth = input_date("Date of Birth: ") | |
person.place_of_birth = input("Place of Birth: ") | |
def add_interest(): | |
global person | |
person.add_interest(input("Interest: ")) | |
def show_biography(): | |
print("Name: {person.forenames} {person.surname}".format(person=person)) | |
if person.maiden_name: | |
print("Maiden Name: {person.maiden_name}".format(person=person)) | |
print("Born in {pob} on {dob}".format(dob=person.date_of_birth.strftime("%d/%m/%Y"), | |
pob=person.place_of_birth)) | |
print("Currently {age} years old".format(age=person.calculate_current_age())) | |
print("Interests include: ") | |
for interest in person.interests: | |
print(" - {interest}".format(interest=interest)) | |
def show_help(): | |
for action_cmd, attributes in actions.items(): | |
print("Type {action} to {help}".format(action=action_cmd, | |
help=attributes["help"])) | |
actions = { | |
"setbasics": | |
{ | |
"call": set_basics, | |
"help": "set the basic details (name, age etc.)" | |
}, | |
"addinterest": | |
{ | |
"call": add_interest, | |
"help": "add an interest (e.g. cooking or skateboarding)" | |
}, | |
"showbio": | |
{ | |
"call": show_biography, | |
"help": "show the biography as it currently exists." | |
}, | |
"help": | |
{ | |
"call": show_help, | |
"help": "show an explanation of how this programme works" | |
}, | |
"close": | |
{ | |
"call": sys.exit, | |
"help": "close this programme" | |
} | |
} | |
if __name__ == "__main__": | |
person = Person() | |
while True: | |
try: | |
action = actions[input(">>> ")] | |
function = action["call"] | |
function() | |
except KeyError: | |
print("Invalid command") | |
show_help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 13. Is that interest part called an array, which is used to store multiple values in one string?