【问题描述】用异常处理改进温度转换程序,使其能够接收并处理用户的异常输入。 在温度刻画的不同体系中,摄氏度以1标准大气压下水的结冰点为0度,沸点为100度。华氏度以1标准大气压下水的结冰点为32度,沸点为212度。如何利用程序辅助进行摄氏度和华氏度之间的转换? 设计算法:根据华氏和摄氏温度定义,其单位刻度对应温度关系为(212-32)/(100-0)=1.8,转换公式如下: C = ( F – 32 ) / 1.8 F = C * 1.8 + 32 【输入形式】输入温度加温度制式的代表字母 【输出形式】转换后的温度加温度制式的代表字母 【样例输入输出1】 What is the temperature?82F The converted temperature is 27C 【样例输入输出2】 What is the temperature?28C The converted temperature is 82F 【样例输入输出3】 What is the temperature?28X End with 'C','c','F','f' 【样例输入输出4】 What is the temperature?ABF Input error,代码是什么
时间: 2023-05-31 11:03:14 浏览: 302
【样例输入输出说明】
样例1:输入82F,表示华氏温度82度,输出27C,表示转换后的摄氏温度为27度。
样例2:输入28C,表示摄氏温度28度,输出82F,表示转换后的华氏温度为82度。
样例3:输入28X,表示输入错误,程序提示输入以C、c、F、f结尾。
样例4:输入ABF,表示输入错误,程序提示输入格式错误。
相关问题
温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit)。请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。转换算法如下:(C+表示摄氏度、F+表示华氏度
以下是一个将华氏度转换为摄氏度的Python程序示例:
```python
temp = input()
if temp[0] == 'F':
fahrenheit = float(temp[1:])
celsius = (fahrenheit - 32) / 1.8
print("C{:.2f}".format(celsius))
```
以下是一个将摄氏度转换为华氏度的Python程序示例:
```python
temp = input()
if temp[0] == 'C':
celsius = float(temp[1:])
fahrenheit = celsius * 1.8 + 32
print("F{:.2f}".format(fahrenheit))
```
温度的刻画有两个不同体系:摄氏度(Celsius)和华氏度(Fabrenheit)。 请编写程序将用户输入华氏度转换为摄氏度,或将输入的摄氏度转换为华氏度。 转换算法如下:(C表示摄氏度、F表示华氏度)
华氏度转摄氏度的公式:$C=(F-32)\times\frac{5}{9}$
摄氏度转华氏度的公式:$F=C\times\frac{9}{5}+32$
可以使用以下 Python 代码进行转换:
```python
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5 / 9
return celsius
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 9 / 5 + 32
return fahrenheit
# 测试华氏度转摄氏度
fahrenheit = float(input("请输入华氏度:"))
celsius = fahrenheit_to_celsius(fahrenheit)
print("华氏度 %.2f 对应的摄氏度为 %.2f" % (fahrenheit, celsius))
# 测试摄氏度转华氏度
celsius = float(input("请输入摄氏度:"))
fahrenheit = celsius_to_fahrenheit(celsius)
print("摄氏度 %.2f 对应的华氏度为 %.2f" % (celsius, fahrenheit))
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)