[Tutor] Flow control question.
Clay Shirky
clay at shirky.com
Mon Aug 11 12:19:26 EDT 2003
As an exercise, I am writing a MAC address tester. It takes a collection of
possible MAC addresses, compares them to a test case, and outputs matches,
doesn't match, and errors for strings that cant be valid MAC addresses.
On these errors, I return with 0, then, in the "check addresses" loop, I try
to 'continue' if the return value is 0, but it isn't working.
The code is
def bad_address(address, error):
print address, "not a valid MAC address:", error, "..."
def test_mac(mac):
address_list = []
elements = re.split('[\:\-\.]', mac)
if len(elements) == 1:
bad_address( mac, "No valid seperator...")
return 0
if len(elements) != 6:
bad_address( mac, "Need 6 two-hex-digit numbers...")
return 0
for element in elements:
num = long(element, 16)
if num >= 256:
bad_address(mac, "Numeric element above 256")
return 0
else:
address_list.append(element)
hex_num = "".join(address_list)
return long(hex_num, 16)
for new_address in new_addresses:
if new_address == 0: continue # I want to skip the next test
if test_mac(new_address) == test_mac(orig_address):
print new_address, "matches", orig_address
else:
print new_address, "doesn't match..."
And when I run it against a test list, every address that fails generates
two errors, both a bad_address() error and a "doesn't match" error from the
for loop below.
How can I make the for loop skip the second if test if the return is after a
bad_address() call?
-clay
More information about the Tutor
mailing list