Home >Backend Development >Python Tutorial >How to convert temperature with python code?

How to convert temperature with python code?

coldplay.xixi
coldplay.xixiOriginal
2020-06-20 15:24:2320021browse

How to convert temperature with python code?

How to convert temperature with python code?

Method for temperature conversion using python code:

Step 1: Analyze the calculation part of the problem

Step 2: Determine the function and use the IPO method for further analysis

Input: Fahrenheit or Celsius temperature value, temperature identifier

Processing: Temperature conversion algorithm

Output :Celsius or Fahrenheit temperature value, temperature identification

F:Fahrenheit temperature C:Celsius

Step 3: Design algorithm

C=(F-32)/1.8;
F=C*1.8+32

Step Four: Write a program

#TempConvert.py
val = input("请输入带温度表示符号的温度值(例如: 32C): ")
if val[-1] in ['C','c']:
    f = 1.8 * float(val[0:-1]) + 32
    print("转换后的温度为: %.2fF"%f)
elif val[-1] in ['F','f']:
    c = (float(val[0:-1]) - 32) / 1.8
    print("转换后的温度为: %.2fC"%c)
else:
    print("输入有误")步骤五:调试、运行程序
       在系统命令行上通过如下命令执行程序:
       C:\>python TempConvert.py

Or: Use IDE to open the above file and press F5 to run (recommended)

How to convert temperature with python code?

Recommended tutorial: " python video tutorial

The above is the detailed content of How to convert temperature with python code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn