码迷,mamicode.com
首页 > 编程语言 > 详细

MVC模式:python案例

时间:2018-02-05 00:32:47      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:return   ike   com   print   for   except   let   else   get   

 

 

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(‘And the quote is: "{}"‘.format(quote))

    def error(self, msg):
        print(‘Error: {}‘.format(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:
            n = self.view.select_quote()
            try:
                n = int(n)
            except ValueError as err:
                self.view.error("Incorrect index ‘{}‘".format(n))
            else:
                valid_input = True
        quote = self.model.get_quote(n)
        self.view.show(quote)


def main():
    controller = QuoteTerminalController()
    while True:
        controller.run()

if __name__ == ‘__main__‘:
    main()

  

MVC模式:python案例

标签:return   ike   com   print   for   except   let   else   get   

原文地址:https://www.cnblogs.com/andy9468/p/8414516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!