这是当初刚学时,练习写的一个写function,用来转换温度。用到了包括if else判断式, while迴圈, def func, format(), float(), str(), input()的基础概念。
#温度转换练习def temp_trans(): while True: whitch_temp = str(input('请输入要转换的温度单位是°C(摄氏)or °F(华氏): ')) if whitch_temp == "C": temp_C = float(input('请输入要转换的温度:')) temp_F = temp_C * 9/5 + 32 print("{num}°{unit}".format(num = temp_C, unit = whitch_temp), "等于 {}°F".format(temp_F)) return elif whitch_temp == "F": temp_F = float(input('请输入要转换的温度:')) temp_C = (temp_F - 32)*5/9 print("%s°%s" %(temp_F, whitch_temp), "等于 %s°C" %(temp_C)) return else: print('请您输入正确的温度单位"C"或"F"') temp_trans()