python学习做业:有效的括号

有效的括号app

给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串,判断字符串是否有效。
括号必须以正确的顺序关闭,”()” 和 “()[]{}” 是有效的可是 “(]” 和 “([)]” 不是
    示例 1:

输入: “()”
输出: true
示例 2:ide

输入: “()[]{}”
输出: true
示例 3:code

输入: “(]”
输出: false
示例 4:orm

输入: “([)]”
输出: false
示例 5:字符串

输入: “{[]}”
输出: trueget

s = input("符号:")
class Stack(object):
    def Is(self,s):
        d = {')': '(', '}': '{', ']': '['}  #赋予符号对应字典
        stack = []
        for char in s:
            if char in '({[':
                stack.append(char)
            elif char in ')}]':
                if not stack:
                    return  False
                else:
                    if stack.pop() != d[char]:
                        return  False
        if stack:
            return  False
        else:
            return True
if __name__ == '__main__':
    print(Stack().Is(s))

商品信息类的封装

from datetime import  datetime
class Medicaine():
    name = ''
    price = 0
    PD = ''
    Exp = ''
    def __init__(self,name,price,PD,Exp):
        self.name =name
        self.price = price
        self.PD = PD
        self.Exp = Exp
    def get_name(self):
        return self.name
    def get_GP(self):
        start = datetime.strptime(self.PD,'%Y-%m-%d')
        end = datetime.strptime(self.Exp,'%Y-%m-%d')
        return (end-start).days
    def get_price(self):
        return   self.price
    def is_expire(self):
        now =   datetime.now()
        end = datetime.strptime(self.Exp,'%Y-%m-%d')
        if now > end:
            return ('已过时')
        else:
            return ('未过时')
if __name__ == '__main__':
    medicaineobj=Medicaine(name='感冒药', price=100, PD='2019-1-10', Exp='2019-12-1')

    print('药品名称:',str(medicaineobj.get_name()))
    print('药品保质期:',str(medicaineobj.get_GP()))
    print('药品是否过时:',(medicaineobj.is_expire()))

银行帐户资金交易管理

import  time
import prettytable as pt
money = 1000
acount_logs = []
class Account:
    def __init__(self):
        global  money
        self.money = money
        self.acount_logs = acount_logs
    def deposit(self):
        amount = float(input('存钱金额:'))
        self.money += amount
        self.write_log(amount,'转入')
    def withdrawl(self):
        amount = float(input('取钱金额:'))
        if amount > self.money:
            print('余额不足')
        else:
            self.money -= amount
            self.write_log(amount,'消费')
    def transaction_log(self):
        tb = pt.PrettyTable()
        tb.field_names = ["交易日期","摘要","金额","币种","余额"]
        for info in self.acount_logs:
            if info[1] =='转入':
                amount = '+{}'.format(info[2])
            else:
                amount = '-{}'.format(info[2])
            tb.add_row([info[0],info[1],amount,'人民币',info[3]])
            print(tb)
    def write_log(self,amout,handle):
        create_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        data =[create_time,handle,amout,self.money]
        self.acount_logs.append(data)
def show_menu():
    """ 显示菜单栏 """
    menu = """
====================银行帐户资金交易管理====================
0: 退出
1:存款
2: 取款
3: 打印交易详情
===========================================================
    """
    print(menu)
if __name__ == '__main__':
    show_menu()
    account = Account()
    while True:
        choice = int(input("请输入您的选择: "))
        if choice == 0:
            exit(0)
            print("退出系统")
        elif choice == 1:
            flag = True
            while flag:
                account.deposit()
                flag = True if input("是否继续存款(Y|N): ").lower()== 'y' else False
        elif choice == 2:
            flag = True
            while flag:
                account.withdrawl()
                flag = True if input("是否继续取款(Y|N): ").lower()== 'y' else False
        elif choice == 3:
            account.transaction_log()
        else:
            print("请选择正确的编号")
相关文章
相关标签/搜索