Python温度转换代码
时间: 2024-04-19 20:22:51 浏览: 20
当然可以!以下是一个简单的Python温度转换代码示例:
```python
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
# 示例使用
temperature_celsius = 25
temperature_fahrenheit = celsius_to_fahrenheit(temperature_celsius)
print(f"{temperature_celsius}摄氏度等于{temperature_fahrenheit}华氏度")
temperature_fahrenheit = 77
temperature_celsius = fahrenheit_to_celsius(temperature_fahrenheit)
print(f"{temperature_fahrenheit}华氏度等于{temperature_celsius}摄氏度")
```
这段代码定义了两个函数,`celsius_to_fahrenheit`用于将摄氏温度转换为华氏温度,`fahrenheit_to_celsius`用于将华氏温度转换为摄氏温度。你可以根据需要调用这两个函数进行温度转换。
相关问题
python温度转换代码
以下是一个用Python编写的温度转换代码:
```
TempStr = input("请输入带有符号的温度值:")
if TempStr[-1 in ['F', 'f']:
C = (eval(TempStr in ['C', 'c']:
F = 1.8 * eval(TempStr
温度转换Python代码
以下是将摄氏温度转换为华氏温度的Python代码:
```python
celsius = float(input("请输入摄氏温度:"))
fahrenheit = (celsius * 1.8) + 32
print("{0:.1f}摄氏度 = {1:.1f}华氏度".format(celsius, fahrenheit))
```
以下是将华氏温度转换为摄氏温度的Python代码:
```python
fahrenheit = float(input("请输入华氏温度:"))
celsius = (fahrenheit - 32) / 1.8
print("{0:.1f}华氏度 = {1:.1f}摄氏度".format(fahrenheit, celsius))
```