编写函数实现摄氏度和华氏度之间的转换。 根据华氏和摄氏温度定义,转换公式如下: C = ( F – 32 ) / 1.8 F = C * 1.8 + 32 其中,C表示摄氏温度,F表示华氏温度。 【输入形式】 共两行,第一行为一个字符。保证字符为c(表示下一行会给出一个摄氏温度,那么你要输出对应的华氏温度)或者f(与c相反)。 第二行为一个浮点数,表示当前给出的温度数值。
时间: 2023-06-18 19:07:27 浏览: 113
以下是Python的实现代码:
```python
def temperature_conversion():
unit = input() # 输入单位
temperature = float(input()) # 输入温度
if unit == 'c':
fahrenheit = temperature * 1.8 + 32 # 摄氏度转华氏度
print("{:.2f}".format(fahrenheit)) # 保留两位小数输出
elif unit == 'f':
celsius = (temperature - 32) / 1.8 # 华氏度转摄氏度
print("{:.2f}".format(celsius)) # 保留两位小数输出
else:
print("输入错误!")
temperature_conversion() # 调用函数进行转换
```
使用示例:
输入:
```
c
25
```
输出:
```
77.00
```
输入:
```
f
98.6
```
输出:
```
37.00
```
阅读全文