Python Forum
Why isalnum() didn't output False when I use punctuations, spaces, etc in input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isalnum() didn't output False when I use punctuations, spaces, etc in input?
#1
Sad 
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()
deanhystad write Jun-14-2024, 08:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py   problemset2.4.py (Size: 1.23 KB / Downloads: 22)
Reply
#2
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.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code to use output from arduino as input (help) Updownrightleft 0 2,312 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft
  Unable to output the indices from an input file rainbow2312 1 3,059 Nov-18-2017, 05:53 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020