Reputation: 1
So here is my code (mind the tiny syntax mistakes:
UserID = input("Please enter your UserID ")
if len(UserID) !=6:
print("Wrong Format")
elif UserID[:1] == (UserID[:1]).lower():
print("Wrong Format")
elif UserID[1:3] == (UserID[1:3]).upper():
print("Wrong Format")
elif UserID[3:] > ord(UserID[3:]):
print("Wrong Format")
else
print("Correct Format")
Basically, the purpose of this program is to have a UserID of 6 characters with one upper case letter, two lower case letters and 3 digits in the format
Abc123
I run into an issue here
elif UserID[3:] > ord(UserID[3:]):
print("Wrong Format")
where the ord() function cannot evaluate the ASCII equivalent of a list. I know it is supposed to be for characters so I am stuck as to what to do. This section of the code is to ensure that any number from the 3rd element onwards is a number so it is less than the ascii equivalent of 9.
Upvotes: 0
Views: 81
Reputation: 180887
Just for reference, here's how you'd validate the whole username with a regex;
import re
if re.match('^[A-Z][a-z]{2}[0-9]{3}$', UserID):
print("Correct Format")
else:
print("Wrong Format")
In your existing code, to check they're all numeric you don't need ord
, you can just compare all characters that they're between 0
and 9
inclusive;
if not all(c >= '0' and c <= '9' for c in UserID[3:]):
print("Wrong format")
Upvotes: 1
Reputation: 6633
Could just be simplified to:
UserID = input("Please enter your UserID ")
if ( (len(UserID) !=6) or UserID[0].islower()
or UserID[1:3].isupper() or not UserID[3:].isdigit() ) :
print("Wrong Format")
else
print("Correct Format")
Upvotes: 0
Reputation: 20336
To see if a string is composed only of integers, you can use str.isdigit()
:
elif not UserID[3:].isdigit():
print("Wrong Format")
Apparently, (from the comments), there are some things for which str.isdigit()
returns True even if it isn't an integer. To fix that, do this:
elif not all(c in "0123456789" for c in UserID[3:]):
print("Wrong Format")
Upvotes: 1