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. Note:If this is wrong place for that, sorry,
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 feed back, 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.
def main():
# Get user input
plate = get_input()
print(plate)
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()
Th1s0neGuy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1