時(shí)間:2023-07-24 03:39:02 | 來源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-07-24 03:39:02 來源:網(wǎng)站運(yùn)營(yíng)
用Python寫個(gè)小工具網(wǎng)頁(yè),就是這么簡(jiǎn)單:簡(jiǎn)簡(jiǎn)單單的用 Python 擼一個(gè)計(jì)算年齡都工具網(wǎng)頁(yè),不用對(duì)前端特別熟悉,只要專注于工具的邏輯,其他都交給 Python 吧。# 用戶輸入的生日now = datetime.strptime(B, "%Y-%m-%d")# 輸出現(xiàn)在年齡delta_y = rd(datetime.now(), now).yearsdelta_m = rd(datetime.now(), now).monthsdelta_d = rd(datetime.now(), now).days
# 檢查日期格式try: val = datetime.strptime(B, "%Y-%m-%d")except: # 如果格式錯(cuò)誤,警告提示 put_error("警告:日期格式不正確") time.sleep(3) continue
# 日期比現(xiàn)在時(shí)間大put_warning( f"找不到結(jié)果,現(xiàn)在日期是" f"{date[0]}-{date[1]}-{date[2]}, " f"你不能使用" f"{in_date[0]}-{in_date[1]}-" f"{in_date[2]}這個(gè)日期。")
# 計(jì)算年齡from dateutil.relativedelta import relativedelta as rd# 獲取時(shí)間、格式化時(shí)間from datetime import datetime# 用來延遲import time# 用來生成網(wǎng)頁(yè)from pywebio.input import *from pywebio.output import *while True: clear() # 每次循環(huán)先清空所有數(shù)據(jù) # 標(biāo)題 put_html("<p align=""left"">" "<h4>年齡計(jì)算器</h4></p>") # 輸入生日日期 B = input( "", placeholder="你的生日,格式為 yyyy-mm-dd" ) # 檢查日期格式 try: val = datetime.strptime(B, "%Y-%m-%d") except: # 如果格式錯(cuò)誤,警告提示 put_error("警告:日期格式不正確") time.sleep(3) continue # 解析輸入的日期 in_date = B.split('-') # 獲取現(xiàn)在時(shí)間 date = datetime.now().strftime("%Y-%m-%d") # 解析現(xiàn)在的日期 date = date.split('-') # 轉(zhuǎn)化為數(shù)字列表 in_date = [int(i) for i in in_date] date = [int(i) for i in date] if in_date <= date: # 用戶輸入的生日 now = datetime.strptime(B, "%Y-%m-%d") # 輸出現(xiàn)在年齡 delta_y = rd(datetime.now(), now).years delta_m = rd(datetime.now(), now).months delta_d = rd(datetime.now(), now).days popup("你的年齡", [put_html( "<h4>%s 歲</br>%s 個(gè)月</br>%s 天</h4>" % (delta_y, delta_m, delta_d)), put_buttons( ['點(diǎn)擊關(guān)閉'], onclick=lambda _: close_popup())], implicit_close=True) else: # 日期比現(xiàn)在時(shí)間大 put_warning( f"找不到結(jié)果,現(xiàn)在日期是" f"{date[0]}-{date[1]}-{date[2]}, " f"你不能使用" f"{in_date[0]}-{in_date[1]}-" f"{in_date[2]}這個(gè)日期。") time.sleep(3) clear()
關(guān)鍵詞:簡(jiǎn)單,小工
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。