Python-Celsius- und Fahrenheit-Temperaturumrechnungsfall

 Anforderungen :

温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fahrenheit)。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
转换算法如下:(C表示摄氏度、F表示华氏度)‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
         C = ( F - 32 ) / 1.8‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
         F = C * 1.8 + 32‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
(1) 输入输出的摄氏度可采用大小写字母C结尾,温度可以是整数或小数,如:12.34C指摄氏度12.34度;‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
(2) 输入输出的华氏度可采用大小写字母F结尾,温度可以是整数或小数,如:87.65F指华氏度87.65度;‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
(3) 输出保留小数点后两位,输入格式错误时,输出提示:输入格式错误;‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
(4) 使用input()获得测试用例输入时,不要增加提示字符串。

 Die erste Art zu schreiben:

while True:
    tem = input("请输入带符号的温度:")
    if tem[-1] == "C" or tem[-1] == "c":
        a = float(tem[0:-1])
        b = a * 1.8 + 32
        c = format(b, ".2f")
        result = str(c)
        print("转换后的温度是:{}F".format(result))
    elif tem[-1] == "F" or tem[-1] == "f":
        a = float(tem[0:-1])
        b = (a - 32) / 1.8
        c = format(b, ".2f")
        result = str(c)
        print("转换后的温度是:{}C".format(result))
    else:
        print("格式输入错误")

Die zweite Schreibweise:

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

 

 

Guess you like

Origin blog.csdn.net/weixin_47401101/article/details/109040876
Recommended