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("請選擇正確的編號")
相關文章
相關標籤/搜索