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:
示例代码 1.3
Python 没有变量类型,函数居然也没有参数类型!太神奇了😮
def tempConvert(ValueStr):
if ValueStr[-1] in ['F', 'f']:
C = (eval(ValueStr[0: -1]) - 32) / 1.8
print("It is equal to {:.2f}C".format(C))
elif ValueStr[-1] in ['C', 'c']:
F = 1.8 * eval(ValueStr[0: -1]) + 32
print("It is equal to {:.2f}F".format(F))
else:
print("Grammatical error!")
TempStr = input("Please input the temperature with the suffix C or F: ")
tempConvert(TempStr)
Output:
思考与练习 2.6
def echo(str):
print(str)
echo(input("Please input a line: "))
ALL RIGHTS RESERVED © 2021 Teddy van Jerry
欢迎转载,转载请注明出处。