Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 94 lines (79 sloc) 3.44 kB
ddd7a38 @humphriesaj userInterface.py now takes a question object as a parameter and not a…
humphriesaj authored
1 #!/usr/bin/python3.1
2
3 __author__ = "Tony Humphries"
4 __date__ = "09/17/12"
5
6 import string
7
8 class UserInterface:
9 """Class which provides the interface for a user to answer quiz
10 questions. The object provides methods for displaying the question and
11 possible answers as well as displaying whether the answer given was
12 correct or incorrect. Special dependencies include ..."""
13
14 def __init__(self, Question):
15 # Set _questionObj to the question object passed in.
16 self._questionObj = Question
17 # This is an index of the alphabet when set.
18 self._userAnswer = None
19 self._alphabet = string.ascii_lowercase
20
21 def _formatChoice(self, choiceIndex, choiceText):
22 """Returns a string with the appropriate letter and formatting
23 before the choice passed to the function. It should be noted
24 that for a given question, the number of choices cannot exceed
25 26, since this is the number of lettersin the English
26 alphabet."""
27 return self._alphabet[choiceIndex]+".)\t"+choiceText
28
9b0a2df @humphriesaj userInterface.py now takes a Question object as a parameter and not a…
humphriesaj authored
29 def _updateQuestion(self, newQuestion):
9ade400 @humphriesaj This version no longer checks for Quiz.getQuestion() to
humphriesaj authored
30 """Updates self._questionObj to be the next question object
9b0a2df @humphriesaj userInterface.py now takes a Question object as a parameter and not a…
humphriesaj authored
31 from the quiz. This new question is passed as a parameter
32 which is a question object."""
33 self._questionObj = newQuestion
ddd7a38 @humphriesaj userInterface.py now takes a question object as a parameter and not a…
humphriesaj authored
34 # This is to prevent the same answer from being submitted
35 # without the user specifying so.
36 self._userAnswer = None
37
9b0a2df @humphriesaj userInterface.py now takes a Question object as a parameter and not a…
humphriesaj authored
38 def updateQuestion(self, newQuestion):
39 self._updateQuestion(newQuestion)
ddd7a38 @humphriesaj userInterface.py now takes a question object as a parameter and not a…
humphriesaj authored
40
41 def welcome(self):
42 """Welcomes the user to the quiz."""
43 print("Welcome to your quiz. Press Enter to continue.\n")
44 input()
45
46 def displayQuestion(self):
47 """Prints the question and possible answers on the screen."""
48 # questionText is a string containing the question to
49 # be displayed.
50 questionText = self._questionObj.getQuestionText()
51 # choices is a tuple of the possible answers to be
52 # displayed in the order they will be displayed on
53 # the screen.
54 choices = self._questionObj.getChoices()
55 # Display the question.
56 print(questionText, '\n')
57 # Display the choices.
58 for i, choice in enumerate(choices):
59 print(self._formatChoice(i, choice))
60
61 def endOfQuiz(self):
62 """Prints a message indicating the user has completed the quiz
63 when called."""
64 print("\nYou have completed the quiz.")
65
66 def prompt(self):
67 """Prompts the user for input on the given question and set
68 _userAnswer to the letter of their choice. Given that the user
69 enters a letter that is not given as a choice, they will be
70 prompted again until that letter is chosen. The user may enter
71 either the lowercase or the uppercase form of the letter with
72 leading and/or trailing whitespace."""
73 answer = ""
74 numOfChoices = len(self._questionObj.getChoices())
75 # Check the answer's length, whether or not it's in the
76 # alphabet, and whether or not it's index in the alphabet is
77 # in the range of choices.
78 while len(answer) != 1 or answer not in self._alphabet or \
79 self._alphabet.index(answer) \
80 not in range(numOfChoices):
81
82 answer = input("\n:> ")
83 answer = answer.lower()
84 answer = answer.strip()
85 # Get the index position of the letter for the final answer.
87f8157 @strasburgerad Fixed some compatablitly errors, now all of the programs work together
strasburgerad authored
86 self._userAnswer = self._questionObj.getChoices()[self._alphabet.index(answer)]
ddd7a38 @humphriesaj userInterface.py now takes a question object as a parameter and not a…
humphriesaj authored
87
88 def getResponse(self):
89 """Returns whether the user entered the correct answer or not.
90 Returns True if the answer was correct, and returns False if
91 the answer was incorrect."""
87f8157 @strasburgerad Fixed some compatablitly errors, now all of the programs work together
strasburgerad authored
92 return self._questionObj.IsCorrect(self._userAnswer)
ddd7a38 @humphriesaj userInterface.py now takes a question object as a parameter and not a…
humphriesaj authored
93
Something went wrong with that request. Please try again.