我是真正的新手,并试图在Python中学习一些编码(只有我的经验来自免费的codeacademy在线练习)。如果输入正确的输入,此代码可正常工作。但是,我想强制“user”(我自己)一次又一次地输入Byte1和Byte2的值,直到最后输入正确的输入。例如,如果我输入两个超出允许范围的值..GGGG,JJJJ,脚本只是说错误并继续。 如果有人有兴趣帮助我解决它,请发表评论或提供有关解决方案的更多细节,以便我了解如何以及为什么。谢谢。
这是代码
import re
def check(X):
if not re.match("^[0-FFFF]*$", X):
print "Error! Only HEX values allowed!"
elif len(X) != 4:
print "Error! Only 4 characters allowed!"
while True:
byte1_start = raw_input("Enter byte1 start value: ")
check(byte1_start)
byte1_end = raw_input("Enter byte1 end value: ")
check(byte1_end)
byte2_start = raw_input("Enter byte2 start value: ")
check(byte2_start)
byte2_end = raw_input("Enter byte2 end value: ")
check(byte2_end)
break
print ("All is ok!")
答案 0 :(得分:4)
你很亲密:
def getValue(cmd):
While True:
answer = raw_input(cmd)
if check(answer):
return answer
else:
print "your input is not valid"
并稍微重写一下check-function:
def check(X):
if not re.match("^[0-FFFF]*$", X):
print "Error! Only HEX values allowed!"
return False
elif len(X) != 4:
print "Error! Only 4 characters allowed!"
return False
return True
您的程序现在读取
byte_1_start = getValue("Enter byte1 start value: ")
byte_1_end = getValue("Enter byte1 end value: ")
...
答案 1 :(得分:1)
您应该check
跟踪可能存在的所有错误,然后返回所有错误。或者只是检查它是否通过 - 不要尝试给出理由。
选项1:
def check(byte):
tests_msgs = [(lambda byte: re.match("^[0-9A-F]*$", byte), "Error! Only HEX values allowed!"),
(lambda byte: len(byte) == 4, "Error! Only 4 characters allowed")]
return [msg for test,msg in tests_msgs if not test(byte)]
bytes = []
for i in range(4):
while True:
byte = raw_input("Enter byte{} start value: ".format(str(i)))
errs = check(byte)
if not errs:
bytes.append(byte)
break
else:
for err in errs:
print err
选项2:
def check(byte):
tests = [lambda byte: re.match("^[0-9A-F]*$", byte,
lambda byte: len(byte) == 4]
return all(test(byte) for test in tests)
bytes = []
for i in range(4):
while True:
byte = raw_input("Enter byte{} start value: ".format(str(i)))
if check(byte):
bytes.append(byte)
break
else:
print "Some generic error message"
另请注意,您的正则表达式无法按预期工作。 [0-FFFF]
不是所有四位十六进制值。您正在寻找:
/^[0-9A-F]*$/i
# re.compile(r"^[0-9A-F]*$", re.I)
但您也可以将这两个错误与一个正则表达式结合起来,然后跳过检查len(X)
:
/^[0-9A-F]{4}$/i
# re.compile(r"^[0-9A-F]{4}$", re.I)
答案 2 :(得分:0)
你可以只使用all来返回一个布尔值和函数,以避免在用户犯一个错误时重复所有输入:
def check(x):
st = {"A", "B", "C", "D", "E", "F", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
if all(ch.upper() in st for ch in x):
if len(x) != 4:
print "Error! Only 4 characters allowed!"
return False
else:
return True
else:
print("Invalid input {}".format(x))
return False
def byte1_start():
inp = raw_input("Enter byte1 start value: ")
while not check(inp):
inp = raw_input("Enter byte1 start value: ")
return inp
def byte1_end():
inp = raw_input("Enter byte1 end value: ")
while not check(inp):
inp = raw_input("Enter byte1 end value: ")
return inp
def byte2_start():
inp = raw_input("Enter byte2 start value: ")
while not check(inp):
inp = raw_input("Enter byte2 start value: ")
return inp
def byte2_end():
inp = raw_input("Enter byte2 end value: ")
while not check(inp):
inp = raw_input("Enter byte2 end value: ")
return inp
for chk in [byte1_start, byte1_end, byte2_start, byte2_end]:
chk()
print("All good")
答案 3 :(得分:0)
导入重新
condition = True
def check(X):
status = True
if not re.match("^[0-FFFF]*$", X):
print "Error! Only HEX values allowed!"
status = False
elif len(X) != 4:
print "Error! Only 4 characters allowed!"
status = False
return status
while condition:
byte1_start = raw_input("Enter byte1 start value: ")
b1s = check(byte1_start)
byte1_end = raw_input("Enter byte1 end value: ")
b1e = check(byte1_end)
byte2_start = raw_input("Enter byte2 start value: ")
b2s = check(byte2_start)
byte2_end = raw_input("Enter byte2 end value: ")
b2e = check(byte2_end)
if (b1s and b1e and b2s and b2e):
condition = False
打印("一切正常!")