[Courses] [python] Object-oriented programming
Leslie
leslie.brothers at verizon.net
Tue Aug 2 03:38:21 UTC 2011
More homework for Lesson 7.
Sorry for these piecemeal responses.
I decided to try exercise 3 (in case it would give me any insight into
exercise 2.)
When I did random sentences in Lesson 5, I had 2 categories of nouns
(animate and inanimate) and each had corresponding verbs. So,for this
exercise, I put the corresponding verbs into each of the two Noun
classes I made.
> __________________
Here is exercise 3. of the homework:
import random
class NounsIn:
def __init__(self, n):
self.noun = n
verbs = [ 'mouldered', 'exploded', 'gyrated', 'vibrated' ]
adjs = [ 'pointless', 'incomprehensible', 'enlightening', 'awesome' ]
def print_words(self):
print self.noun, random.choice(NounsIn.verbs)+". It
was",(random.choice (NounsIn.adjs))+"."
class NounsAn:
def __init__(self, n):
self.noun = n
verbs = [ 'cogitated', 'flipped out', 'vegetated', 'procrastinated',
'ruminated' ]
adjs = [ 'pointless', 'incomprehensible', 'enlightening', 'awesome' ]
def print_words(self):
print self.noun, random.choice(NounsAn.verbs)+". It
was",(random.choice (NounsAn.adjs))+"."
nouns = [ 'nounsan', 'nounsinan' ]
nounsan = [ 'Fred', 'Pablo Escobar', 'My aunt', 'Dixie' ]
nounsinan = ['The moon', 'A water balloon', 'The traffic lights on
Sunset' ]
while True:
x = raw_input("(Hit enter to generate sentences, 'q' to quit.)\n")
if x == 'q':
print "Done."
break
y = random.choice(nouns)
if y == 'nounsan':
noun1 = NounsAn(random.choice(nounsan))
noun1.print_words()
else:
noun1 = NounsIn(random.choice(nounsinan))
noun1.print_words()
> _____________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
More information about the Courses
mailing list