I'm just learning Python and am looking for some feedback on some of the code exercises I've completed and do work. In particular, what would make them more Pythonic, or more closely align with standard practices or what-not.
Here is code I wrote for a loop exercise that works. It gets a license plate number from user, and checks that its formatted correctly. As I said, looking for feedback, but for right now, not looking to import anything, just better ways of writing what I've written. Btw, it's for, I suppose, a popular course, but it's already been turned in, so not trying to cheat, just learn, so any feedback will be appreciated.
Edit: ThoughtThought maybe I should add details of what the code needed to check for to determine if plates were valid or not.
“All vanity plates must start with at least two letters.”
“… vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
“Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.”
“No periods, spaces, or punctuation marks are allowed.”
def main():
# Get user input
plate = get_input()
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def get_input():
text = input("Enter plate text:")
return text
def is_valid(s):
#Check that plate is 2-6 character and starts with 2 letters.
if len(s) >1 and len(s) <7 and s[0].isalpha() and s[1].isalpha():
pass
else:
return False
#check that all text is alpha numeric
for c in s:
if c.isalnum() == True:
continue
else:
return False
#Check that first number is not 0 and that there are no letters after a number
i =0
while i < len(s)-1:
if s[i].isdigit() and s[i +1].isalpha() or s[i].isalpha() and s[i+1] == "0":
return False
i+=1
return True
main()