Jun-26-2024, 08:02 AM
(This post was last modified: Jun-26-2024, 08:03 AM by murmurgeneral.)
(Jun-14-2024, 09:28 PM)deanhystad Wrote: isalnum() does return False for spaces.
for letter in "aA1 :/": print(letter, letter.isalnum())Your problem must be something else.
Output:a True A True 1 True False : False / False
You use slices, but you don't use them effectively. For example, this:
for i in s[0:2]: if i.isnumeric(): return FalseCould be written like this:
if any(c.isnumeric() for c in s[:2] return FalseOr better yet
if not s[:2].isalpha(): return FalseSimilarly, this loop should be removed:
for k in s: if k.isalphanum(): ... else: return FalseAnd replaced withL
if not s.isalnum(): return FalseYour program is also difficult to read because there is so much distance between a test and the function returning a False result because of the test, It would be easier to read if written like this:
pseudo code
def valid_plate(plate): if length of plate not valid: return False if all characters are not alphanumeric: return False if the first two characters are not alphabetic: return False if the first number is "0": return False if any charcter following the first number is not a number return False return True # plate passed all the tests.
I appreciate you sharing this information. I find it handy.
boxing random