# wk02_0914_變數與運算式(1)
1. 變數
2. 運算式
3. input()、print()
# [Inclass practice]
Qusetion{綜合演練5}BMI值稱為身體質量指標,是一個簡易判斷身體胖瘦程度的方法。計算BMI值的攻勢式體重(單位為公斤)除以身高(單位為公尺)的平方:
BMI = 體重(KG)/身高(m)**2
請幫忙設計一個程式讓使用者輸入他的身高(公分)及體重(公斤)後計算出他的BMI值。
```python
Weight = int(input("請輸入體重(KG)"))
Height = int(input("請輸入身高(cm)"))
Height2 = Height/100
Height3 = Height2**2
BMI = Weight/Height3
print("你的BMI是%5.2f" %(BMI))
```
請輸入體重(KG)70
請輸入身高(cm)170
你的BMI是24.22
```python
height = 160
weight = 59
bmi = 59/1.6**2
print("您的身高",height,"您的體重",weight,"您的BMI是",bmi)
```
您的身高 160 您的體重 59 您的BMI是 23.046874999999996
```python
height = 160
weight = 59
bmi = 59/1.6**2
# print("您的身高",height,"您的體重",weight,"您的BMI是",bmi, sep="---", end="===*")
print("您的身高%dcm,您的體重%dkg,您的BMI是%5.2f" %(height, weight, bmi))
```
您的身高160cm,您的體重59kg,您的BMI是23.05
```python
my_height = input("請輸入你的身高(cm)")
print(type(my_height)) #str
my_height=int(my_height)
print(type(my_height)) #int
```
請輸入你的身高(cm)100
<class 'str'>
<class 'int'>
{範例}
1. 格式化列印成績單 <format>
2. 計算成績總分 <input>
3. 計算梯形面積 <arith>
4. 計算複利本金 <complex>
```python
a = 100
b = 23.08
c = "DD"
d = True
print(type(a))
print(type(b))
print(type(c))
print(type(d))
```
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
```python
# 2. 計算成績總分 <input>
chinese = int(input("國文成績?"))
english = int(input("英文成績?"))
print("總分是%d分" %(chinese+english))
```
國文成績?100
英文成績?60
總分是160分
```python
# 3. 計算梯形面積 <arith>
top = 1
bottom = 2
height = 5
area = (top+bottom)*height/2
print("梯形面積為%d"%area)
```
梯形面積為7
# [afterclass practice]
(C) 1. 下列何者是Python 的註解符號? (A) $ (B) // (C) # (D) %
(A) 2. 下列何者是錯誤的變數名稱? (A) if (B) mary (C) str56 (D) error_i
(D) 3. num =8+True,num的值為何? (A) 0 (B) 1 (C) 8 (D) 9
```python
num=8+True
print(num)
```
9
(B) 4. print(type(56.0))顯示的結果為何? (A) str (B) float (C) int (D) double
```python
print(type(56.0))
```
<class 'float'>
(A) 5. 下列何者錯誤? (A) print(23 + "67") (B) print(23+int("67")) (C) print(str(23)+"67") (D) print(str(23)+str("67"))
```python
print(23 + "67")
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[12], line 1
----> 1 print(23 + "67")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
(B) 6. num = 96%5,num的值為何? (A) O (B) 1 (C) 19 (D) 20
```python
num = 96%5
print(num)
```
1
(D) 7. num=5,則num**=3的值為何? (A) 3 (B) 15 (C)25 (D) 125
```python
num = 5
num **= 3
print(num)
```
125
(B) 8. print("78"+"12")的结果為何? (A) 90 (B) 7812 (C) 66 (D)產生錯誤
```python
print("78"+"12")
```
7812
(A) 9. print(78+12)的結果為何? (A) 90 (B) 7812 (C) 66 (D) 產生錯誤
```python
print(78+12)
```
90
(A) 10.下列何者運算子的優先順序最高? (A) -(負) (B)* (C) and (D) +=
# [self practice]
梯形面積=(上底+下底)*高/2,設計程式輸入梯形的上底、下底及高後計算梯形面積。
```python
top = float(input("請輸入梯形上底長度(cm)"))
bottom = float(input("請輸入梯形下底長度(cm)"))
height = float(input("請輸入梯形高度(cm)"))
area=(top+bottom)*height/2
print("梯形面積為%5.2f (cm^2)"%(area))
```
請輸入梯形上底長度(cm)50
請輸入梯形下底長度(cm)100
請輸入梯形高度(cm)30
梯形面積為2250.00 (cm^2)
{綜合演練實作題ch02 Q1} 一年一班只有2位同學,設計程式讓老師分別輸入2位同學的姓名及成績,然後計算成績總分,最後以下圖格式列印。
```python
name1=str(input("請輸入第一位學生的姓名: "))
print(" ")
score1=int(input("請輸入第一位學生的成績: "))
print(" ")
name2=str(input("請輸入第二位學生的姓名: "))
print(" ")
score2=int(input("請輸入第二位學生的成績: "))
print(" ")
print("姓名 成績")
print("%s %d"%(name1,score1))
print("%s %d"%(name2,score2))
print("成績總分為:",(score1+score2))
```
請輸入第一位學生的姓名: 張家誠
請輸入第一位學生的成績: 78
請輸入第二位學生的姓名: 邵競賢
請輸入第二位學生的成績: 100
姓名 成績
張家誠 78
邵競賢 100
成績總分為: 178
{綜合演練實作題ch02 Q2} 計程車計費方式第1公里為70元,多1公里加收30元。設計程式讓運將輸入乘客的公里數。然後計算乘車費用。
```python
km = int(input("請輸入路程公里數(整數):"))
price = 70 + (km-1)*30
print("你的車程車資費為:%d"%(price))