n个人平均分配100元钱,用户输入由n不同取值构成的列表,输出各种情况每人所分数额(分行输出,保留2位小数)。如果发生ZeroDivisionError则输出can't be 0;其他错误输出Error。列表元素处理完毕后输出over。要求:用try语句处理异常
时间: 2024-02-16 22:03:17 浏览: 25
好的,我理解你的问题了。这里是一个可能的解决方案:
```python
while True:
try:
n_list = input().split() # 输入由n不同取值构成的列表
if not n_list: # 如果列表为空则结束程序
print("over")
break
for n in n_list:
n = int(n)
if n == 0: # 如果n为0则输出can't be 0
print("can't be 0")
else:
result = round(100/n, 2) # 平均分配100元钱
print(result)
except ZeroDivisionError: # 处理ZeroDivisionError异常
print("can't be 0")
except Exception as e: # 处理其他异常
print("Error")
```
这个程序会不断循环,每次读取一行输入。如果输入是空行,则程序结束。对于每个输入的n,程序会尝试将其转换为整数,然后计算每人分配的金额,并输出结果。如果n为0,则输出"can't be 0";如果发生ZeroDivisionError异常,则也输出"can't be 0";如果发生其他异常,则输出"Error"。最后,如果输入为空,则输出"over"结束程序。
相关推荐
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)