侧边栏壁纸
  • 累计撰写 197 篇文章
  • 累计收到 496 条评论

Python学习笔记之面向对象商店

2019-10-14 / 0 评论 / 48 阅读
class Cx():
    zhanghao = ''
    __listvar = {
        'aa':'123123',
        'bb':'321321'
    }
    __login = False
    __countError = 0
    __shopy = [
        (1,'鞋子',100,500),
        (2,'袜子',2,1000)
    ]
    __qian = 1000

    def __init__(self):
        self.__vlidate_zhanghao();
        self.__vlidate_password();
        if not self.__login:
            print('系统错误')
            import sys;
            sys.exit();
        self.__showShopy();
        self.__goumai();

    def __vlidate_zhanghao(self):
        zhanghao = False;
        while not zhanghao:
            name = input('请输入您的账号:').strip();
            if name in self.__listvar:
                zhanghao = True;
                self.zhanghao = name
            if not zhanghao:
                print('对不起账号不存在请重新输入')

    def __vlidate_password(self):
        mima = False;
        while not mima:
            if self.__countError > 2:
                print('对不起您输入错误次数已达上限,系统自动退出')
                import sys;
                sys.exit();
            pwd = input('请输入您的密码:')
            if pwd == self.__listvar[self.zhanghao]:
                mima = True
                print('恭喜登陆成功')
                self.__login = True
            else:
                print('您输入的密码不正确请重新输入')
                self.__countError += 1;

    def __showShopy(self):
        print('商品信息')
        for i in self.__shopy:
            print('序号%d  名称:%s  价格:%d  剩余:%d' % (i[0], i[1], i[2], i[3]))
        print('输入数字 0 离开商店')

    def __goumai(self):
        while True:
            cz = False;
            dd = input('请输入需要购买商品的序号:');
            if dd == '0':
                print('正在送您离开商店。。。')
                import sys;
                print('您已离开商店!')
                sys.exit()
            if (dd == ''):
                print('您的输入有误,请重新输入')
                continue;
            dd = dd.strip()

            for i in self.__shopy:
                if dd == str(i[0]):
                    cz = True
                    temp = i;
            if not cz:
                print('对不起,您输入的序号不正确请重试')
                continue;
            if self.__qian < temp[2]:
                print('对不起,您的余额不足以购买本商品')
                continue;

            self.__qian = self.__qian - temp[2]
            self.__showShopy();
            print('恭喜您购买[%s]成功, 花费[%d] 剩余[%d]' % (temp[1], temp[2], self.__qian))

Cx()

评论一下?

OωO
取消