python温度转换程序

1.使用pycharm运行温度转换程序,尝试将温度单位设在前面

2.参照温度转换程序,自己写一个关于货币转换、长度转换、重量转换或者面积转换的程序

循环+函数

def convertemperature():
    temperature = ""
    while (temperature != "q"):
        temperature = input("请输入带有符号的温度:")
        if (temperature[-1] in ['f', 'F']):
            C = (eval(temperature[0:-1]) - 32) / 1.8
            print("转换后的温度是{:.2f}C".format(C))
            print("退出请输入q")
        elif (temperature[-1] in ['c', 'C']):
            C = (1.8 * eval(temperature[0:-1]) + 32)
            print("转换后的温度是{:.2f}C".format(C))
        else:
            print("输入格式错误")
            print("退出请输入q")

convertemperature()
# 循环 +异常处理 温度单位设在前面 f代表华氏度 c代表摄氏度---函数里面循环 
def convertemperature():
    temperature = ""
    while (temperature != "exit"):
        temperature = input("请输入带有符号的温度:")
        if (temperature[0] in ['f', 'F']):
            try:
                C = (eval(temperature[1:]) - 32) / 1.8
                print("转换后的温度是{:.2f}C".format(C))
            except:
                print("转换失败,请重新输入")
            print("退出请输入exit")
        elif (temperature[0] in ['c', 'C']):
            try:
                C = (1.8 * eval(temperature[1:]) + 32)
                print("转换后的温度是{:.2f}C".format(C))
            except:
                print("转换失败,请重新输入")
            print("退出请输入exit")
        else:
            print("输入格式错误")
            print("退出请输入exit")
    # 循环 +异常处理 温度单位设在前面 f代表华氏度 c代表摄氏度
convertemperature()

循环里面使用函数

temperature=""
while (temperature != "q"):
    temperature = input("请输入带有符号的温度:")
    convertemperature2(temperature)


    def convertemperature2(aa):
        temperature = aa
        if (temperature[0] in ['f', 'F']):
            try:
                C = (eval(temperature[1:]) - 32) / 1.8
                print("转换后的温度是{:.2f}C".format(C))
            except:
                print("转换失败,请重新输入")
            print("退出请输入q")
        elif (temperature[0] in ['c', 'C']):
            try:
                C = (1.8 * eval(temperature[1:]) + 32)
                print("转换后的温度是{:.2f}C".format(C))
            except:
                print("转换失败,请重新输入")
            print("退出请输入q")
        else:
            print("输入格式错误")
            print("退出请输入q")

重量转换

# 货币转换、长度转换 、重量转换或者面积转换
temperature = ""
while temperature != "q":
    temperature = input("请输入带有符号的重量:")
    print(temperature[-2:])
    if temperature[-2:] in ['kg', "Kg", "KG", "kG"]:
        try:
            C = (eval(temperature[0:-2])  * 1000)
            print("转换后的重量是{:.2f}克".format(C))
        except:
            print("转换失败,请重新输入")
        print("退出请输入q")
    elif temperature[-1] in ['g', 'G']:
        try:
            C = (eval(temperature[0:-1]) / 1000)
            print("转换后的重量是{:.2f}千克".format(C))
        except:
            print("转换失败,请重新输入")
        print("退出请输入q")
    else:
        print("输入格式错误")
        print("退出请输入q")
# 循环 +异常处理  重量转换

你可能感兴趣的:(Python,开发语言,python,开发语言)