-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmvc.py
55 lines (42 loc) · 1.35 KB
/
mvc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
quotes = (
"A man is not complete until he is married. Then he is finished.",
"As I said before, I never repeat myself.",
"Behind a successful man is an exhausted woman.",
"Black holes really suck...",
"Facts are stubborn things.",
)
class QuoteModel:
def get_quote(self, n):
try:
value = quotes[n]
except IndexError as err:
value = "Not found!"
return value
class QuoteTerminalView:
def show(self, quote):
print(f'And the quote is: "{quote}"')
def error(self, msg):
print(f"Error: {msg}")
def select_quote(self):
return input("Which quote number would you like to see? ")
class QuoteTerminalController:
def __init__(self):
self.model = QuoteModel()
self.view = QuoteTerminalView()
def run(self):
valid_input = False
while not valid_input:
try:
n = self.view.select_quote()
n = int(n)
valid_input = True
except ValueError as err:
self.view.error(f"Incorrect index '{n}'")
quote = self.model.get_quote(n)
self.view.show(quote)
def main():
controller = QuoteTerminalController()
while True:
controller.run()
if __name__ == "__main__":
main()