[Courses] [python] Object-oriented programming

Leslie leslie.brothers at verizon.net
Wed Aug 3 21:58:23 UTC 2011


Homework, exercise 2.
(I decided not to give up. I think I am starting to get it.)


import random

class Quiz:
	SEwordPairs = {}
	
	def __init__(self, s, e):
		self.SEwordPairs[s] = e
		
	def show_Spanish_flashcards(self):
		while True:
			x = random.choice(self.SEwordPairs.keys())
			y = self.SEwordPairs[x]
			ans = raw_input("What is the English translation of %s?\n" % (x))
			if ans == y:
				print "Correct!"
			else:
				print "The correct answer was", y+"\n"
			c = raw_input("Press 'enter' to continue or 'q' to quit.\n")
			if c == 'q':
				break
				return()
		
	def show_English_flashcards(self):
		while True:
			x = random.choice(self.SEwordPairs.keys())
			y = self.SEwordPairs[x]
			ans = raw_input("What is the Spanish translation of %s?\n" % (y))
			if ans == x:
				print "Correct!"
			else:
				print "The correct answer was", x+"\n"
			c = raw_input("Press 'enter' to continue or 'q' to quit.\n")
			if c == 'q':
				break
				return()		


while True:	
	# make dictionary objects
	x = raw_input("Enter word in Spanish or type 'q' to quit and go to the
quiz.\n")
	if x == 'q':
		break
	y = raw_input("Enter its English translation.\n")
	entry = Quiz(x,y)

# show flashcards
print "Now we go to the quiz.\n"
x = raw_input("Press 's' to see the quiz in Spanish, 'e' to see it in
English.\n")
if x == 's':
	entry.show_Spanish_flashcards()
elif x == 'e':
	entry.show_English_flashcards()
else:
	print "I didn't understand your input."


	
-Leslie









More information about the Courses mailing list