- 帖子
- 2
- 精华
- 0
- 积分
- 10
- 阅读权限
- 10
- 注册时间
- 2018-10-19
- 最后登录
- 2021-1-13
|
#! /usr/bin/python
# -*- coding: utf-8 -*-
import re
# ip_rule = r'(?<![\.\d])(?:25[0-5]\.|2[0-4]\d\.|[01]?\d\d?\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?![\.\d])'
value = '输入合法的远程主机ip:'
ip_rule = r"^(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
# retry=3,递减1,3次后break
def setup_reinput_ip_3(values, retry=3):
val = raw_input(values)
print val
# re.match 如果匹配成功,则返回一个Match,否则返回一个None
while re.match(ip_rule, val) is None:
print 'retry1', retry
if retry == 0:
print 'retry2=', retry
break
return 'wrong' # 它的存在,只是为了试错
print 'retry7', retry
else:
print 'retry3', retry
retry -= 1
print 'retry4', retry
setup_reinput_ip_3('请输入合法的远程ip地址: ' + '(剩余 {} 次)'.format(retry), retry)
print 'retry5', retry
else:
print 'retry6', retry
return val
print setup_reinput_ip_3('输入远程主机ip:')
请问这个代码哪里出了问题?
为什么不匹配的输入结果不按照思路break呢?
它还会继续执行几次。
我的运行结果,粘贴如下:
输入远程主机ip:9
9
retry1 3
retry3 3
retry4 2
请输入合法的远程ip地址: (剩余 2 次)9
9
retry1 2
retry3 2
retry4 1
请输入合法的远程ip地址: (剩余 1 次)9
9
retry1 1
retry3 1
retry4 0
请输入合法的远程ip地址: (剩余 0 次)9
9
retry1 0
retry2= 0
retry5 0
retry1 0
retry2= 0
retry5 1
retry1 1
retry3 1
retry4 0
请输入合法的远程ip地址: (剩余 0 次)9
9
retry1 0
retry2= 0
retry5 0
retry1 0
retry2= 0
retry5 2
retry1 2
retry3 2
retry4 1
请输入合法的远程ip地址: (剩余 1 次)9
9
retry1 1
retry3 1
retry4 0
请输入合法的远程ip地址: (剩余 0 次)9
9
retry1 0
retry2= 0
retry5 0
retry1 0
retry2= 0
retry5 1
retry1 1
retry3 1
retry4 0
请输入合法的远程ip地址: (剩余 0 次)9
9
retry1 0
retry2= 0
retry5 0
retry1 0
retry2= 0
None
Process finished with exit code 0
|
|