Problem-Based Learning
Python 3
Lecture: 01-variable
Online Python
• [Link]
compiler/
Lecture Notes
• [Link]
Problem
• Write a program which • 顯示提示
prompts the user for a Celsius • 輸入數字
temperature, convert the
temperature to Fahrenheit, and • 計算
9
print out the converted 𝐹 = 𝐶 × + 21
temperature. 5
• 顯示結果
顯示
• print(3.14) • 3.14
• print("Temperature C") • Temperature C
• print(2*3.14) • 6.28
• print(30*9/5+32) • 86.0
變數使用
• a = 3.14 • 3.14
• print(a)
• a = "Temperature C" • Temperature C
• print(a)
• a = 30*9/5+32 • 86.0
• print(a)
變數使用
• a = "Temperature:" • Temperature:C=30
• b = 30 • Temperature:F=86.0
• c = b * 9/5 +32
• print(a+"C=",b)
• print(a+"F=",c)
輸入
• input("Temperature C:") • Temperature C:
• a = "Temperature C:" • Temperature C:
• C = input(a)
• print(C)
• print(C+32) • ERROR!
• print(type(C)) • <class 'str'>
輸入
• a = "Temperature C" • Temperature C
• C = input(a) • 30 輸入
• C = float(C) • 62.0
• print(C+32)
輸入
• a = "Temperature C" • Temperature C
• C = input(a) • 30
• try: • 62.0
• C = float(C)
• print(C+32)
• except:
• print("ERROR!")
4個空格或是Tab
術語
電腦: 人:
• 字串(string) • 文字(text)
• 浮點數(float) • 實數(real number)
Problem
• Write a program which • 顯示提示
prompts the user for a Celsius • 輸入數字
temperature, convert the
temperature to Fahrenheit, and • 計算
9
print out the converted 𝐹 = 𝐶 × + 21
temperature. 5
• 顯示結果
Solution
• C = input("Temperature C= ")
• try:
• C = float(C)
• F = C * 9/5 + 32
• print("Temperature F= ", F)
• except:
• print("ERROR!")