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: 27)
Reply


Messages In This Thread
Why isalnum() didn't output False when I use punctuations, spaces, etc in input? - by ardavegok - Jun-14-2024, 08:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Code to use output from arduino as input (help) Updownrightleft 0 2,315 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft
  Unable to output the indices from an input file rainbow2312 1 3,068 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