编写函数——温度转换

最后输出的只有一个值,到底是怎么转换的?
凑字数:只要心中有坚定的信念,目光所及之处,便是一往无前。

img

步骤过程的代码


def convert(f = None, c = None):
    if f == None and c != None:
        f = 9.0 / 5.0 * c + 32
    elif f != None and c == None:
        c = (f - 32) * 5.0 / 9.0
    
    return f, c
    

degstr = input("")

ds = degstr.split('=')

temptype = ds[0]
temp = float(ds[1])

c = None
f = None

if temptype == 'c':
    c = temp
elif temptype == 'f':
    f = temp

C, F = convert(f, c)

if temptype == 'c':
    print("%.2fC<--->%.2fF"%(round(F, 2), round(C, 2)))
elif temptype == 'f':
    print("%.2fF<--->%.2fC"%(round(C, 2), round(F, 2)))


def convert(f = None, c=None):
    if f==None:
        f = round(c*9/5+32,2)
        print(f'{float(c)}C<--->{f}F')
    else:
        c =  round((f-32)*5/9,2)
        print(f'{float(f)}F<--->{c}C')
src = input("请输入温度:")
if src.startswith("c"):
    convert(c = int(src.split('=')[1]))
else:
    convert(f = int(src.split('=')[1]))

img


如有帮助,请采纳!

def convert(f = None, c = None):
    if f:
        return "{}F<--->{}C".format(round(float(f), 2), round((f - 32) * 5 / 9, 2))
    else:
        return "{}C<--->{}F".format(round(float(c), 2), round(9 * c / 5 + 32, 2))
    
res = convert(f = -45)
print(res)