14.编写一个函数,接收一个10位的数字串为参数,判断第7位到9位组成的三位数能否被5整除
时间: 2024-03-19 21:43:13 浏览: 20
可以使用以下代码编写一个函数,来判断第7位到9位组成的三位数能否被5整除:
```python
def check_num(num_str):
if len(num_str) == 10:
sub_num_str = num_str[6:9]
if sub_num_str.isdigit():
sub_num = int(sub_num_str)
if sub_num % 5 == 0:
return True
return False
```
函数接收一个10位数字串作为参数,首先判断数字串的长度是否为10,如果是,则取出第7位到第9位的子串,并检查该子串是否为数字。如果是数字,则将其转化为整数,并判断是否能被5整除,如果能,则返回True,否则返回False。
可以使用以下代码测试该函数:
```python
num_str = '1234567890'
if check_num(num_str):
print("第7位到9位组成的三位数能被5整除")
else:
print("第7位到9位组成的三位数不能被5整除")
```
输出结果为:第7位到9位组成的三位数不能被5整除。
注意,这里假设数字串中的第7位到第9位一定是数字。如果存在非数字字符,则需要进行额外的处理。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)