简易练习-1 温度单位转换

这是当初刚学时,练习写的一个写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()

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章