【Python】 Python 語言設計基礎 - 第二章
阿新 • • 發佈:2021-02-17
Before we comb through the codes
《Python 語言程式設計基礎》 高等教育出版社 - 第二版
Python Version 3.9.0 + Visual Studio 2019 / Visual Studio Code 1.52.1
Preparation
【Python】 下載 Python(Visual Studio 2019)的步驟
【Python】 Python 語言設計基礎 - 第一章
示例程式碼 1.1
TempStr = input("Please input the temperature with the suffix C or F: " )
if TempStr[-1] in ['F', 'f']:
C = (eval(TempStr[0: -1]) - 32) / 1.8
print("It is equal to {:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
F = 1.8 * eval(TempStr[0: -1]) + 32
print("It is equal to {:.2f}F".format(F))
else:
print("Grammatical error!" )
Output:
示例程式碼 1.2
給一種和書上不完全一樣的方法
TempStr = 'Y'
while True:
TempStr = input("Please input the temperature with the suffix C or F. (Enter N to exit)\nTemperature: ")
if TempStr[-1] in ['N', 'n']:
break
if TempStr[-1] in ['F', 'f']:
C = (eval(TempStr[0: -1]) - 32 ) / 1.8
print("It is equal to {:.2f}C".format(C))
elif TempStr[-1] in ['C', 'c']:
F = 1.8 * eval(TempStr[0: -1]) + 32
print("It is equal to {:.2f}F".format(F))
else:
print("Grammatical error!")
Output:
ALL RIGHTS RESERVED © 2021 Teddy van Jerry
歡迎轉載,轉載請註明出處。