---
# System prepended metadata

title: 第十堂課-函數-模組
tags: [Python]

---

###### tags: `Python`

# 第十堂課-函數-模組

### 函數-模組
test1 
```python=
def msg():
    print("Hello World!!")
    
def msg2(name):
    print("Hello," +name)
```
```python=
import test1 as t                      #使用as設定test1的別名
t.msg()                                #呼叫函數
t.msg2("Jackey")
```
:::spoiler 執行結果
![](https://i.imgur.com/fx29kHm.png)
:::
```python=
def msg():
    print("Hello World!!")
```
```python=
from test1 import msg                  #指定要呼叫的函數
msg()
```
:::spoiler 執行結果
![](https://i.imgur.com/bWjKMrL.png)
:::
```python=
import random     

for i in range(3):                     #執行3次
    print(random.randint(1,6))         #執行亂數範圍:1~6
```
:::spoiler 執行結果
![](https://i.imgur.com/YqXo2hT.png)
:::
:::
```python=
def m1(m, f, a):
    print("總成績:%0.f" %(m * 0.3 +f * 0.4 +a *0.3))
    
def m2(m, f, a, hw):
    print("總成績:%0.f" %(m * 0.3 +f * 0.3 +a * 0.3 +hw * 0.1))
```
```python=
import score as sc  
#使用as設定score的別名
n = int(input("1.假日班  2.日間部 "))    

if n == 1:                             #使用者輸入1時，執行
    mid = int(input("期中考:"))         
    final = int(input("期末考:"))       
    attend = int(input("出席:"))        
    sc.m1(mid, final, attend)

elif n == 2:                           #使用者輸入2時，執行
    mid = int(input("期中考:"))         
    final = int(input("期末考:"))       
    attend = int(input("出席:"))       
    hw = int(input("作業:"))            
    sc.m2(mid, final, attend, hw)
```
:::spoiler 執行結果
![](https://i.imgur.com/mVuhlgT.png)
:::
