Skip to content

Instantly share code, notes, and snippets.

@willmcc
Created February 17, 2011 01:29
Show Gist options
  • Save willmcc/830741 to your computer and use it in GitHub Desktop.
Save willmcc/830741 to your computer and use it in GitHub Desktop.
Self-Commenting Code in Python
class PartyLocation:
BAR = 1
RESTAURANT = 2
def choosePartyLocation(members_of_party):
drinkers = []
non_drinkers = []
for i in range( len(members_of_party) ):
if(willDrink(person)):
drinkers.append(person)
else:
non_drinkers.append(person)
if len(drinkers) > len(non_drinkers):
return PartyLocation.BAR
else:
return PartyLocation.RESTAURANT
def willDrink(person)
DRINKING_AGE = 21
can_drink = person['age'] >= DRINKING_AGE
will_drink = person['drinks']
return can_drink and will_drink
def myPartyLocation(desired_location, work_tomorrow):
if party_location == PartyLocation.BAR and not work_tomorrow:
return PartyLocation.BAR
elif party_location == PartyLocation.RESTAURANT:
return PartyLocation.RESTAURANT
def printPartyLocation(party_location)
if(party_location == PartyLocation.BAR):
print 'I will go to the bar'
else:
print 'I will go to the restaurant'
paulo = {'name' : 'Paulo', 'age' : 23, 'drinks' : True }
peter = {'name' : 'Peter', 'age' : 21, 'drinks' : False }
james = {'name' : 'James', 'age' : 20, 'drinks' : False }
carlos = {'name' : 'Carlos', 'age' : 18, 'drinks' : False }
party_of_people = [paulo, peter, james, carlos]
party_location = choosePartyLocation(party_of_people)
work_tomorrow = False
party_location = myPartyLocation(party_location, work_tomorrow)
printPartyLocation(party_location)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment