### wk11_1116_函式與模組、期末專題參考 - 7.2 數值函式 - 7.3 字串函式 - 7.4 亂數模組 - 7.5 時間模組 #### 【inclass practice】 ###### {範例} 攝氏溫度轉華氏溫度 \ 學生分蘋果 \ 總和及排序 \ 檢查網址格式 \ 以字串排版函式列印成績單 \ 轉換日期格式 \ 擲骰子遊戲 \ 大樂透中獎號碼 \ 列印時間函式所有資訊 \ 計算執行一百萬次整數運算的時間 \ ```python import time as t print("hello......") t.sleep(0.3) print("Ray") ``` hello...... Ray ```python from time import sleep, ctime, localtime, perf_counter print("hello") sleep(0.3) print("Ray") ``` hello Ray ```python from time import sleep, ctime, localtime, perf_counter startTime = perf_counter() for i in range(11) : print(i) endTime = perf_counter() print(f"總運算時間, {endTime - startTime}") ``` 0 1 2 3 4 5 6 7 8 9 10 總運算時間, 0.0005811997689306736 ```python startTime = perf_counter() for i in range(11) : print(i) sleep(0.86) endTime = perf_counter() print(f"總運算時間, {endTime - startTime}") ``` 0 1 2 3 4 5 6 7 8 9 10 總運算時間, 9.46914659999311 ```python time1 = localtime() time2 = ctime() print(time1,"\n", time2) ``` time.struct_time(tm_year=2023, tm_mon=11, tm_mday=16, tm_hour=10, tm_min=58, tm_sec=12, tm_wday=3, tm_yday=320, tm_isdst=0) Thu Nov 16 10:58:12 2023 ```python import random as r print(r.randint(1,6)) ``` 4 ```python import random as r inkey = input("enter結束,按任意鍵後enter開始擲") if len(inkey) > 0 : print(r.randint(1,6)) else : print("Game over") ``` enter結束,按任意鍵後enter開始擲5 3 #Github ```python ! pip install Pandas Numpy #安裝套件 ``` Requirement already satisfied: Pandas in c:\users\cjc11\anaconda3\lib\site-packages (1.5.3) Requirement already satisfied: Numpy in c:\users\cjc11\anaconda3\lib\site-packages (1.24.3) Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\cjc11\anaconda3\lib\site-packages (from Pandas) (2.8.2) Requirement already satisfied: pytz>=2020.1 in c:\users\cjc11\anaconda3\lib\site-packages (from Pandas) (2022.7) Requirement already satisfied: six>=1.5 in c:\users\cjc11\anaconda3\lib\site-packages (from python-dateutil>=2.8.1->Pandas) (1.16.0) #### 【afterclass practice】 - 教學影音: 新手入門 #07 Function (函式) 新手入門 #09 Module (模組) - 期末分組名單 #### 【selfpractice】 以十二小時(上午、下午)顯示現在時刻。 ```python import datetime # 取得當前時間 now = datetime.datetime.now() # 將24小時制轉換為12小時制 formatted_time = now.strftime("%I:%M %p") # %I: 小時(12小時制), %M: 分鐘, %p: 上午或下午 print("現在時刻是:", formatted_time) ``` 現在時刻是: 11:34 AM ```python import time as t time1 = t.localtime() show = "現在時刻:" if time1.tm_hour < 12: show += "上午 " hour = time1.tm_hour else: show += "下午 " hour = time1.tm_hour - 12 show += str(hour) + " 點 " + str(time1.tm_min) + " 分 " show += str(time1.tm_sec) + " 秒" print(show) ``` 現在時刻:上午 11 點 34 分 32 秒 ```python innum = 0 list1 = [] for i in range(0,4): innum = int(input("請輸入第 " + str(i+1) + " 位同學分數:")) list1.append(innum) print("最高分為:%d" % max(list1)) print("最低分為:%d" % min(list1)) print("總分為:%d" % sum(list1)) print("平均為:%6.2f" % (sum(list1)/4)) ``` 請輸入第 1 位同學分數:90 請輸入第 2 位同學分數:89 請輸入第 3 位同學分數:75 請輸入第 4 位同學分數:97 最高分為:97 最低分為:75 總分為:351 平均為: 87.75