【Python程式設計】小程式 貨幣轉換 v2
阿新 • • 發佈:2019-02-10
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "YanZanG" over = False from color_me import ColorMe class Info(object): def __init__(self, name, age, wages, Sex, phone): self.name = name self.age = age self.wages = wages self.Sex = Sex self.phone = phone def info(self): print("恭喜您,通過認證,您的基本資訊如下:".center(50,"=")) print(f""" 姓名:{self.name} 年齡:{self.age} 工資:{self.wages} 性別:{self.Sex} 電話:{self.phone} """) class Money(object): """貨幣轉換系統""" def rmb(self): """人民幣轉換美元...""" option = True while (option): User_Input = input("請您輸入您需要轉換的金額,人民幣:(元) 退出:Q :".strip()) if User_Input.strip() == "": User_Input_warning = ColorMe("警告:輸入的金額不能為空,請您重新輸入!!!").red() print(User_Input_warning) elif User_Input == "q": User_Input_q = ColorMe("警告:檢測輸入q為小寫,請您重新輸入!!!").red() print(User_Input_q) elif User_Input == "Q": print("歡迎您再次使用人民幣轉換工具,再見~".center(50, "-")) option = over elif User_Input.endswith("元") == False: User_Input_end = ColorMe("警告:輸入的人民幣以'元'結尾,請您重新輸入!!!").red() print(User_Input_end) else: your_money = int(User_Input[:User_Input.index("元")]) rmb = your_money / 6.6 rmb = round(rmb, 2) Conversion_results = (f"您需要轉換的人民幣為{User_Input} 轉換為美元結果為:{rmb}$(按Q退出!!!)") print(Conversion_results) def dollar(self): """美元轉換人民幣...""" option = True while (option): User_Input = input("請您輸入您需要轉換的金額,美元:($) 退出:Q:".strip()) if User_Input.strip() == "": User_Input_warning = ColorMe("警告:輸入的金額不能為空,請您重新輸入!!!").red() print(User_Input_warning) elif User_Input == "q": User_Input_q = ColorMe("警告:檢測輸入q為小寫,請您重新輸入!!!").red() print(User_Input_q) elif User_Input == "Q": print("歡迎您再次使用美元轉換工具,再見~".center(50, "-")) option = over elif User_Input.endswith("$") == False: User_Input_end = ColorMe("警告:輸入的美元以'$'結尾,請您重新輸入!!!").red() print(User_Input_end) else: your_money = int(User_Input[:User_Input.index("$")]) rmb = your_money * 6.6 rmb = round(rmb, 2) Conversion_results = (f"您需要轉換的美元為{User_Input} 轉換為人民幣結果為:{rmb}$(按Q退出!!!)") print(Conversion_results) class Transfer(Info,Money): def __init__(self, name, age, wages, Sex, phone, height): Info.__init__(self, name, age, wages, Sex, phone) Money.__init__(self) self.height = height def name_info(self): return result.info() def user_input(self): Really = True while (Really): menu_dict = { "1" : "人民幣轉美元程式", "2" : "美元轉人民幣程式", "Q" : "退出此程式" } for k, v in menu_dict.items(): print(f"{k}、{v}") user_result = input("請您選擇你需要的業務:".strip()) if user_result == "1": self.rmb() elif user_result == "2": self.dollar() elif user_result == "Q": print("歡迎您再次使用,再見!".center(50,"-")) Really = False else: print("請您輸入(1/2/Q)") def User_info(): OB = True while (OB): print(f"歡迎來到{__author__}翻譯小程式,我們需要進一步瞭解您的資訊!!!".center(50,"-")) Your_name = input("請您輸入您的名字:".strip()) Your_age = input("請您輸入您的年齡:".strip()) Your_wages = input("請您輸入您的工資:".strip()) Your_Sex = input("請您輸入您的性別:".strip()) Your_phone = input("請您輸入您的電話:".strip()) Your_height = input("請您輸入您的身高:".strip()) if Your_name.strip() == "": User_Input_n = ColorMe("警告:姓名不能為空,請您重新輸入!!!").red() print(User_Input_n) elif Your_age.strip() == "": User_Input_a = ColorMe("警告:年齡不能為空,請您重新輸入!!!").red() print(User_Input_a) elif Your_wages.strip() == "": User_Input_w = ColorMe("警告:工資不能為空,請您重新輸入!!!").red() print(User_Input_w) elif Your_Sex.strip() == "": User_Input_s = ColorMe("警告:性別不能為空,請您重新輸入!!!").red() print(User_Input_s) elif Your_phone.strip() == "": User_Input_p = ColorMe("警告:電話不能為空,請您重新輸入!!!").red() print(User_Input_s) elif Your_height.strip() == "": User_Input_h = ColorMe("警告:身高不能為空,請您重新輸入!!!").red() print(User_Input_h) else: result = Transfer(Your_name, Your_age, Your_wages, Your_Sex, Your_phone,Your_height) result.info() result.user_input() OB = False User_info()
#!/usr/bin/env Python #-*- coding:utf-8 -*- __author__ = 'De8ug' class ColorMe: """ give me color see see... 實際用起來很簡單: ColorMe('somestr').blue() """ def __init__(self, some_str): self.color_str = some_str def blue(self): str_list = ["\033[34;1m", self.color_str, "\033[0m"] return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m" def green(self): str_list = ["\033[32;1m", self.color_str, "\033[0m"] return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m" def yellow(self): str_list = ["\033[33;1m", self.color_str, "\033[0m"] return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m" def red(self): str_list = ["\033[31;1m", self.color_str, "\033[0m"] return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m" def main(): ColorMe('somestr').blue() if __name__ == '__main__': main()