Python example 1: Temperature conversion

The conversion algorithm is as follows: (C means Celsius, F means Fahrenheit) ‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬ ‪‬‪‬‮‬‪‬‪‬

C = ( F - 32 ) / 1.8‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

F = C * 1.8 + 32‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

Requirements are as follows ‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

(1) The input and output degrees Celsius can end with a capital or uppercase letter C, and the temperature can be an integer or a decimal, for example: 12.34C means 12.34 degrees Celsius; ‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪ ‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

(2) The input and output Fahrenheit can end with a capital or uppercase letter F, and the temperature can be an integer or a decimal, for example: 87.65F means 87.65 degrees Fahrenheit; ‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪ ‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

(3) The output retains two decimal places. When the input format is incorrect, the output prompts: Input format error; ‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬ ‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

(4) When using input() to obtain test case input, do not add a prompt string. ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬ ‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

tempstr=input()
if tempstr[-1] in ['F','f']:
    c=(eval(TempStr[0:-1])-32)/1.8
    print("{:.2f}C".format(c))
elif tempstr[-1] in ['C','c']:
    f=eval(tempstr[0:-1])*1.8+32
    print("{:.2f}F".format(f))
else : print("输入格式错误")

Knowledge points elicited
Insert image description hereInsert image description hereInsert image description here

Guess you like

Origin blog.csdn.net/weixin_50925658/article/details/113720058