[Courses] [python] Object-oriented programming
lenore borisova
orangegirl at gmail.com
Wed Aug 10 09:32:14 UTC 2011
Problem 1:
1
2 import random, sys
3
4 class Flashcard :
5
6 count = 0
7
8 def __init__(self, q, a) :
9 self.question = q
10 self.answer = a
11
12 def print_question(self) :
13 print self.question
14
15 line = "============================================="
16
17 def quiz_user(self) :
18 print self.line
19 self.print_question()
20 print self.line
21 ans = raw_input("What is the name of this element? ")
22
23 if ans.strip().lower() == self.answer.strip().lower() :
24 print "Good job!"
25 Flashcard.count += 1
26 return Flashcard.count
27
28 if ans.strip().lower() == "q" :
29 print "Goodbye! You answered",
30 print Flashcard.count,
31 print "correctly."
32 raise SystemExit
33
34 print "Sorry, the answer was:", self.answer
35 return False
36
37 cards = [
38 Flashcard("He", "helium"),
39 Flashcard("N", "nitrogen"),
40 Flashcard("Se", "selenium"),
41 Flashcard("Bi", "bismuth"),
42 Flashcard("Na", "sodium"),
43 Flashcard("Rb", "rubidium"),
44 Flashcard("Sn", "tin"),
45 Flashcard("Mg", "magnesium"),
46 Flashcard("Os", "osmium"),
47 ]
48
49 while True :
50 random.choice(cards).quiz_user()
Problem 2:
1
2
3 import random, sys
4
5 class Flashcard :
6
7 count = 0
8
9 def __init__(self, q, a) :
10 self.question = q
11 self.answer = a
12
13 def print_question(self) :
14 print self.question
15
16 line = "============================================="
17
18 def quiz_user(self) :
19
20 print self.line
21 self.print_question()
22 print self.line
23 ans = raw_input("How do you say this number in
Japanese? ")
24
25 if ans.strip().lower() == self.answer.strip().lower()
:
26 print "Good job!"
27 Flashcard.count += 1
28 return Flashcard.count
29
30 if ans.strip().lower() == "q" :
31 print "Goodbye! You answered",
32 print Flashcard.count,
33 print "correctly."
34 raise SystemExit
35
36 print "Sorry, the answer was:", self.answer
37 return False
38
39 cards = [
40 Flashcard("1", "ichi"),
41 Flashcard("2", "ni"),
42 Flashcard("3", "san"),
43 Flashcard("4", "shi"),
44 Flashcard("5", "go"),
45 ]
46
47 test = raw_input("Enter c for create flashcards, p to quiz yourself, or
q to quit: ")
48
49
50 if test == "q" :
51 print "Goodbye!"
52 raise SystemExit
53
54 if test == "c" :
55 q = raw_input("Enter integer: ")
56 a = raw_input("Enter translation: ")
57 cards.append(Flashcard(q,a))
58 print "Flashcard has been created! Now quiz yourself!"
59
60 while True :
61 random.choice(cards).quiz_user()
The above works. However, I tried a variation below that does not.
When I tried to create 2 classes for Problem 2, it runs although it doesn't
appear to actually append the item to the cards list or to keep it in the
list because it doesn't show up in the quiz afterward this way (but does in
the previous version). I should get sleep so I'm going to stop here for the
night (morning, now) and continue this when I can free up more time.
1
2
3 import random, sys
4
5
6 class Quiz :
7
8 def __init__(self, q, a) :
9 self.question = q
10 self.answer = a
11
12 def print_question(self) :
13 print self.question
14
15 print "JAPANESE NUMBERS GAME\n"
16 print "Create Flashcards: Example 1 and ichi\n"
17
18 q = raw_input("Enter an integer: ")
19 a = raw_input("Enter the English translation: ")
20
21
22 def __add__(self,q,a):
23
24 self.cards.append(Flashcard(q,a))
25
26 print "Flashcard has been created. Let's quiz you!\n"
27
28
29 class Flashcard :
30
31 count = 0
32
33 def __init__(self, q, a) :
34 self.question = q
35 self.answer = a
36
37 def print_question(self) :
38 print self.question
39
40 line = "============================================="
41
42 def quiz_user(self) :
43
44 print self.line
45 self.print_question()
46 print self.line
47 ans = raw_input("How do you say this number in
Japanese? ")
48
49 if ans.strip().lower() == self.answer.strip().lower()
:
50 print "Good job!"
51 Flashcard.count += 1
52 return Flashcard.count
53
54 if ans.strip().lower() == "q" :
55 print "Goodbye! You answered",
56 print Flashcard.count,
57 print "correctly."
58 raise SystemExit
59
60 print "Sorry, the answer was:", self.answer
61 return False
62
63 cards = [
64 Flashcard("1", "ichi"),
65 Flashcard("2", "ni"),
66 Flashcard("3", "san"),
67 Flashcard("4", "shi"),
68 Flashcard("5", "go"),
69 ]
70
71 while True : 72 random.choice(cards).quiz_user()
Lenore
More information about the Courses
mailing list