Python Forum
Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Why isalnum() didn't output False when I use punctuations, spaces, etc in input? (/thread-42310.html)



Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - ardavegok - Jun-14-2024

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")

#Much like a list, a str is a “sequence” (of characters), which means it can be “sliced” into shorter strings with syntax like s[i:j]. For instance, if s is "CS50", then s[0:2] would be "CS".
#str.isalnum() eturn True if all characters in the string are alphanumeric and there is at least one character, False otherwise.  
# Alfanümerik, A ile Z arasındaki harfler ile 0 – 9 arasındaki rakamları kapsayan karakter topluluğudur
def is_valid(s):
    n=0
    if 2<=len(s)<=6:
        for k in s:
            if k.isalnum():
                for i in s[0:2]:
                    if i.isnumeric():
                        return False
                for j in s:
                    if n==0 and j=="0":
                       return False
                    else:
                        if j.isnumeric():
                            n=1
                            
                        elif n==1 and j.isnumeric()==False:
                            return False
            
            else: 
                return False
            return True
    else:
        return False
    
main()



RE: Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - deanhystad - Jun-14-2024

isalnum() does return False for spaces.
for letter in "aA1 :/":
    print(letter, letter.isalnum())
Output:
a True A True 1 True False : False / False
Your problem must be something else.

You use slices, but you don't use them effectively. For example, this:
for i in s[0:2]:
    if i.isnumeric():
        return False
Could be written like this:
if any(c.isnumeric() for c in s[:2]
    return False
Or better yet
if not s[:2].isalpha():
    return False
Similarly, this loop should be removed:
for k in s:
    if k.isalphanum():
        ...
    else:
        return False
And replaced withL
if not s.isalnum():
    return False
Your 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.



RE: Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - murmurgeneral - Jun-26-2024

(Jun-14-2024, 09:28 PM)deanhystad Wrote: isalnum() does return False for spaces.
for letter in "aA1 :/":
    print(letter, letter.isalnum())
Output:
a True A True 1 True False : False / False
Your problem must be something else.

You use slices, but you don't use them effectively. For example, this:
for i in s[0:2]:
    if i.isnumeric():
        return False
Could be written like this:
if any(c.isnumeric() for c in s[:2]
    return False
Or better yet
if not s[:2].isalpha():
    return False
Similarly, this loop should be removed:
for k in s:
    if k.isalphanum():
        ...
    else:
        return False
And replaced withL
if not s.isalnum():
    return False
Your 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